<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理
use think\Image;
use app\admin\model\File;

class Product extends Auth
{
    public function __construct(Request $request){
        $this->getChildIDdata = [];
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }

    public function productlist(Request $request)
    {   
        if(isset($this->data['noSave'])) return false;

        $join = [
                    ['multi_lang zu_mu','zu.mkey = zu_mu.mkey','LEFT'],
                    // ['pro_img i','zu.id = i.pro_id','LEFT'],
                    ['pro_brand b','zu.brand_id = b.id','LEFT'],
                ];

        // $field = [
        //     'zu.*,i.img_url,i.id img_id,b.name brand_name,zu_mu.content pro_name'
        // ];
        $field = [
            'zu.*,b.name brand_name,zu_mu.content pro_name'
        ];
        $where['zu.is_item'] = 0;
        $where['zu_mu.lang'] = $this->lang;
        // $where['i.prochar_id'] = 0;
        // $where['i.is_icon'] = 2;
        // $where['i.drank'] = ['<',2];
        if(isset($this->data['item']) && $this->data['item']){
            $where['zu.is_item'] = $this->data['item'];
        }

        // 查找
        if(isset($this->data['searchText']) && $this->data['searchText']){  
            $where['zu.id|zu_mu.content'] = ['like',"%".$this->data['searchText'].'%'];   
            $searchText = $this->data['searchText'];
        }else{
            $searchText = '';
        }

        if(isset($this->data['listStatus']) && $this->data['listStatus'] !=9){
            $where['zu.status'] = $this->data['listStatus'];
        }

        $productList = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
        // dump($productList);die;
        $productList = array_map(function ($item){
            $res = Db::name('pro_img')->where(['pro_id'=>$item['id'],'prochar_id'=>0])->group('pro_id')->select();
            if($res){
                $item['img_url'] = $res[0]['img_url'];
            }
            return $item;
        },$productList);
        // dump($productList);die;
        $returnData['size'] = count( $productList);
        // 获取分页数据
        if(isset($this->data['page']) && isset($this->data['limit'])){
            $productList = array_slice($productList,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
            $returnData['page'] = $this->data['page'];
        }else{
            $returnData['page'] =1;
        }

        foreach ($productList as $k => $v) {
            $productList[$k]['create_name'] = getinfo($v['create_id'], 'admin')['nickname'];//编辑者
            $productList[$k]['create_at'] = getTimeDates($v['create_at'],9,$this->lang);
            // $productList[$k]['pro_img_path'] = $v['img_url'].$v['img_id'];     // 拼产品图片url
            $productList[$k]['product_role_count'] = hasData('prochar',['pro_id'=>$v['id']]);    
            $productList[$k]['update_user_id'] = $this->user_id;
            $productList[$k]['status_name'] = $v['status'] == 1 ? lang('normal') : lang(getViewMultiLanguage($v['status']));
            $productList[$k]['is_sale'] = $v['is_sale'] == 1 ? lang('yes') : lang('no');
        } 

        // 获取全部分类
        $cateData = getSearch('procat','zu',['zu.*,mu.content'],['zu.status'=>1,'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
        $cateData = getTree($cateData);

        // dump( $product_cate_data );

        if($request->isAjax()){
            $returnData['cateData'] = $cateData;
            $returnData['productList'] = $productList;
            $returnData['searchText'] = $searchText;
            return json_encode($returnData);
        }else{
            return view('list',['productList'=>$productList]);   
        }
    }

    // 概要
    public function list_open(Request $request)
    {   
        if(isset($this->data['request'])){
            return view('listopen',['id'=>$this->data['id']]);   
        }else{
            return json_encode($this->listOpenAndCopy($this->data));
        }
    }

    function listOpenAndCopy($data){
        $join = [
                    ['multi_lang zu_mu','zu.mkey = zu_mu.mkey','LEFT'],
                    ['pro_img i','zu.id = i.pro_id','LEFT'],
                    ['pro_brand b','zu.brand_id = b.id',"LEFT"],
                    // ['prodesc c','zu.id = c.pro_id','LEFT']
                ];

        $field = [
            'zu.*,i.img_url,i.id img_id,i.img_format,b.name brand_name,zu_mu.content pro_name'
        ];
        // dump($this->lang);die;
        $where['zu_mu.lang'] = $this->lang;
        // $where['c.lang'] = $this->lang;
        $where['zu.id'] = $data['id'];

        $checkRead = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
        // return $checkRead;
        $cateChild = '';
        foreach ($checkRead as $k1 => $v1) {
            $checkRead[$k1]['prodesc'] = getValue('prodesc',['pro_id'=>$data['id'],'lang'=>$this->lang],'content');   
            $checkRead[$k1]['pro_img_path'] = $v1['img_url'].$v1['img_id'].'.'.$v1['img_format'];     // 拼产品图片url
            $cateChildarr = getSearch('pro2cate','zu',['mu.content cateChild'],['zu.pro_id'=>$v1['id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
            foreach ($cateChildarr as $k2 => $v2) {
                $cateChild .= $v2['cateChild'].',';
            }
            $checkRead[$k1]['cateChild'] = rtrim($cateChild,',');

        }
        
        $checkRead_roleWhere['status'] = ['in', '1,3,4'];
        $checkRead_roleWhere['pro_id'] = $data['id'];
        $checkRead_role = getDatas('prochar',['*'],$checkRead_roleWhere,'id desc');
        // return $checkRead_role;
        foreach ($checkRead_role as $k3 => $v3) {
            $checkRead_role[$k3]['ctn_note'] = getValue('multi_lang',['mkey'=>$v3['ctn_note'],'lang'=>$this->lang],'content');
            $checkRead_role[$k3]['status'] = $v3['status'] == 1 ? lang('normal') : lang(getViewMultiLanguage($v3['status']));
            $checkRead_role[$k3]['create_name'] =  getinfo($v3['create_id'], 'admin')['nickname'];//编辑者
            $checkRead_role[$k3]['create_at'] = getTimeDates($v3['create_at'],9,$this->lang);
            $feature = $this->getFeature($v3['pro_id'],$v3['id'],$this->lang);
            $checkRead_role[$k3]['feature_value'] = $feature['feature_value'];
            $feature_name = $feature['feature_name'];
        }
        $returnData['feature_name'] = $feature_name;
        $returnData['checkRead'] = empty($checkRead) ? [] : $checkRead[0];
        $returnData['checkRead_role'] = $checkRead_role;
        return $returnData;
    }

    // 产品复制
    public function proCopy(Request $request)
    {   
        if($this->data['request'] == 'ajax'){
            $returnData = $this->listOpenAndCopy($this->data);
            // dump($returnData);die;
            if(!empty($returnData['checkRead_role'])){
                foreach ($returnData['checkRead_role'] as $k => $v) {
                    
                    // dump($v['feature_value']);die;
                    if(!empty($v['feature_name'])){
                        foreach ($v['feature_name'] as $k1 => $v1) {
                            $returnData['checkRead_role'][$k]['feature'][$k1]['name'] = $v1;
                            // dump(getValue('multi_lang',['mkey'=>['like','%proattr_%'],'content'=>$v1,'lang'=>$this->lang],'mkey'));die;
                            $returnData['checkRead_role'][$k]['feature'][$k1]['name_id'] = explode('_', getValue('multi_lang',['mkey'=>['like','%proattr_%'],'content'=>$v1,'lang'=>$this->lang],'mkey'))[1];
                            $returnData['checkRead_role'][$k]['feature'][$k1]['value'] = $v['feature_value'][$k1];
                            $returnData['checkRead_role'][$k]['feature'][$k1]['value_id'] = explode('_', getValue('multi_lang',['mkey'=>['like','%proattr_value_%'],'content'=>$v['feature_value'][$k1],'lang'=>$this->lang],'mkey'))[2];
                        }
                    }else{
                        $returnData['checkRead_role'][$k]['feature'] = [];
                    }
                    unset($returnData['checkRead_role'][$k]['feature_name']);
                    unset($returnData['checkRead_role'][$k]['feature_value']);
                    
                }
            }
            $returnData['brandList'] = getDatas('pro_brand',['*'],'1=1');
            // dump($returnData);die;
            return json_encode($returnData);
        }else{
             return view('procopy',['id'=>$this->data['id']]); 
        }
    }

    // 产品复制保存
    public function proCopyInsert(){
        // Db::startTrans();
        // try{
            if(hasData('multi_lang',['mkey'=>['like','%pro_%'],'content'=>$this->data['checkRead']['pro_name']])) return json_encode(array('status'=>3,'msg'=>lang('product_name').'：'.$this->data['checkRead']['pro_name'].' '.lang('already_exists')));

            $pro_insert['brand_id'] = $this->data['checkRead']['brand_id'];
            $pro_insert['is_public'] = $this->data['checkRead']['is_public'];
            $pro_insert['is_sale'] = $this->data['checkRead']['is_sale'];
            $pro_insert['is_item'] = $this->data['checkRead']['is_item'];
            $pro_insert['status'] = 4;
            $pro_insert['create_id'] = $this->user_id;
            $pro_insert['create_at'] = time();
            $pro_insert['pulloff_day'] = $this->data['checkRead']['pulloff_day'];
            $pro_insert['recom_day'] = $this->data['checkRead']['recom_day'];
            $pro_insert['new_day'] = $this->data['checkRead']['new_day'];
            $copyInsert = insertData('pro',$pro_insert);
            if(!$copyInsert) return json_encode(array('status'=>0,'msg'=>lang('product_name').'：'.$this->data['checkRead']['pro_name'].' '.lang('add_failed')));

            // 获取语言表数据
            $lang_cate = getLanguage();

            // 新增相关联的产品角色
            if(empty($this->data['checkRead_role'])) return json_encode(array('status'=>2,'msg'=>lang('product_name').'：'.$this->data['checkRead']['pro_name'].' '.lang('add_failed').' '.lang('missing_role_information')));
            foreach ($this->data['checkRead_role'] as $k => $v) {
                if(hasData('prochar',['model'=>$this->data['checkRead_role'][$k]['model']])) return json_encode(array('status'=>3,'msg'=>lang('the_role_of_product').'：'.$this->data['checkRead_role'][$k]['model'].' '.lang('already_exists')));

                $prochar['pro_id'] = $copyInsert;
                $prochar['model'] = $v['model'];
                $prochar['package_qty'] = $v['package_qty'];
                $prochar['ctn_kgs'] = $v['ctn_kgs'];
                $prochar['ctn_m3'] = $v['ctn_m3'];
                $prochar['ctn_size'] = $v['ctn_size'];
                $prochar['new_day'] = $v['new_day'];
                $prochar['pulloff_day'] = $v['pulloff_day'];
                $prochar['recom_day'] = $v['recom_day'];
                $prochar['status'] = 4;
                $prochar_insert = insertData('prochar',$prochar);
                if(!$prochar_insert) return json_encode(array('status'=>0,'msg'=>lang('product_name').'：'.$this->data['checkRead']['pro_name'].'产品角色：'.$v['model'].' '.lang('add_failed')));

                // 添加产品说，多语言
                foreach ($lang_cate as $k1 => $v1) {
                    $res = insertData('multi_lang',['mkey'=>'prochar_note_'.$prochar_insert,'content'=>$v['ctn_note'],'lang'=>$v1['code']]);
                    if($res){
                        $res_ = updateData('prochar',['id'=>$prochar_insert],['mkey'=>'prochar_note_'.$prochar_insert,'ctn_note'=>'prochar_note_'.$prochar_insert]);
                    }
                }

                // 添加产品角色特征
                if(!empty($v['feature'])){
                    foreach ($v['feature'] as $k1 => $v1) {
                        $featureInsertData['pro_id'] = $copyInsert;
                        $featureInsertData['prochar_id'] = $prochar_insert;
                        $featureInsertData['profeature_id'] = $v1['name_id'];
                        $featureInsertData['profeature_value_id'] = $v1['value_id'];
                        $res1 = insertData('profeature2char',$featureInsertData);
                    }
                }
                

                // 获取被复制产品的介绍类型内容。
                $prochar_cat_desc = getDatas('prodesc',['*'],['pro_id'=>$this->data['checkRead']['id'],'prochar_id'=>$v['id'],'status'=>1]);
                if(!empty($prochar_cat_desc)){
                    foreach ($prochar_cat_desc as $k2 => $v2) {
                        unset($prochar_cat_desc[$k2]['id']);
                        $prochar_cat_desc[$k2]['pro_id'] = $copyInsert;
                        $prochar_cat_desc[$k2]['prochar_id'] = $prochar_insert;
                        $prochar_cat_desc[$k2]['create_id'] = $this->user_id;
                        $prochar_cat_desc[$k2]['create_at'] = time();
                        $prochar_cat_desc[$k2]['update_id'] = 0;
                        $prochar_cat_desc[$k2]['update_at'] = '';
                        $res2 = insertData('prodesc',$prochar_cat_desc);
                    }
                }
            }

            // 添加产品名称多语言，产品基本介绍内容
            foreach ($lang_cate as $k3=> $v3) {
                // 产品名信息
                $pro_multi['mkey'] = 'pro_'.$copyInsert;
                $pro_multi['lang'] = $v3['code'];
                $pro_multi['content'] = $this->data['checkRead']['pro_name'];
                $res3 = insertData('multi_lang',$pro_multi);
                // 产品名称多语言mkey绑定
                $res3 = updateData('pro',['id'=>$copyInsert],['mkey'=>'pro_'.$copyInsert]);

                // 产品介绍信息
                $prodesc['pro_id'] = $copyInsert;
                $prodesc['prodesc_cat_id'] = 0;
                $prodesc['prochar_id'] = 0;
                $prodesc['status'] = 1;
                $prodesc['create_id'] = $this->user_id;
                $prodesc['create_at'] = time();
                $prodesc['verify'] = 2;
                $prodesc['lang'] = $v3['code'];
                $prodesc['content'] = $this->data['checkRead']['prodesc'];
                $res4 = insertData('prodesc',$prodesc);
            }
            // 添加产品分类。
            $pro_cat = $this->data['checkRead']['cateChild'] == '' ? '' : explode(',', $this->data['checkRead']['cateChild']);
            if(!empty($pro_cat)){
                foreach ($pro_cat as $k4 => $v4) {
                    $pro_cat_id = getValue('multi_lang',['mkey'=>['like','%procat_%'],'content'=>$v4],'mkey');
                    $prpcat_id = explode('_', $pro_cat_id)[1];
                    if($pro_cat_id) $res5 = insertData('pro2cate',['pro_id'=>$copyInsert,'procat_id'=>$prpcat_id,'mkey'=>'procat_'.$prpcat_id]);
                }
            }

            // 添加一个系统默认的产品图片后期再编辑图片
            $proImgInsert = insertData('pro_img',['pro_id'=>$copyInsert,'width'=>1000,'height'=>1000,'img_format'=>'png','img_url'=>'/static/Admin/img/nt_logo.png','create_id'=>$this->user_id,'create_at'=>time(),'drank'=>0,'is_icon'=>2]);
            if($copyInsert && $prochar_insert){
                Db::commit();
                return json_encode(array('status'=>1,'pro_id'=>$copyInsert,'msg'=>lang('product_name').'：'.$this->data['checkRead']['pro_name'].' '.lang('copy_added_successfully')));
            }else{
                Db::rollback();
                return json_encode(array('status'=>0,'msg'=>lang('product_name').'：'.$this->data['checkRead']['pro_name'].' '.lang('copy_addition_failed')));
            }
        // }catch(\Exception $e){
        //     Db::rollback();
        //     return json_encode(array('status'=>500,'msg'=>lang('系统报错mysql')));
        // }
    }

    // 获取特征详细信息
    function getFeature($pro_id,$prochar_id,$lang)
    {   

        // 获取当前产品各产品角色绑定的产品特征名称
        $profeaturename =Db::name('profeature2char')->field(['profeature_id'])->where('pro_id',$pro_id)->group('profeature_id')->select();
        foreach ($profeaturename as $k1 => $v1) {
            $profeaturename[$k1] = $v1['profeature_id'];
        }

        $profeature2char = getDatas('profeature2char',['*'],['prochar_id'=>$prochar_id]);
        if(!empty($profeature2char)){
            foreach ($profeature2char as $k => $v) {
                foreach ($profeaturename as $k2 => $v2) {
                    if($v['profeature_id'] == $v2){
                        $feature_name[$k2] = getValue('multi_lang',['mkey'=>'proattr_'.$v['profeature_id'],'lang'=>$lang],'content');
                        $feature_value[$k2] = getValue('multi_lang',['mkey'=>'proattr_value_'.$v['profeature_value_id'],'lang'=>$lang],'content');
                    }
                }
            }
        }else{
            $feature_name = [];
            $feature_value= [];
        }
        
        $returnData['feature_name'] = $feature_name;
        $returnData['feature_value'] = $feature_value;
        // dump($returnData);die;
        return $returnData;
    }

    // 产品详情
    public function productDetail(){
        if($this->data['id']){
            $join = [
                        ['multi_lang zu_mu','zu.mkey = zu_mu.mkey','LEFT'],
                        ['pro_brand b','zu.brand_id = b.id','LEFT'],
                        ['prodesc c','zu.id = c.pro_id','LEFT']
                    ];

            $field = [
                'zu.*,b.name brand_name,zu_mu.content pro_name,c.content prodesc'
            ];
            $where['zu_mu.lang'] = is_lang();
            $where['c.lang'] = is_lang();
            $where['zu.id'] = $this->data['id'];
            $where['c.prodesc_cat_id'] = 1;
            // $where['i.prochar_id'] = 0;
            // $where['i.is_icon'] = 2;

            $checkRead = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
            if(!empty($checkRead)) $checkRead = $checkRead[0];
            // dump($checkRead);die;
            $cateChild = '';

            $checkRead['img_url'] = getValue('pro_img',['pro_id'=>$this->data['id'],'prochar_id'=>0],'img_url');
            $cateChildarr = getSearch('pro2cate','zu',['mu.content cateChild'],['zu.pro_id'=>$checkRead['id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
            if(!empty($cateChildarr)){
                foreach ($cateChildarr as $k2 => $v2) {
                    $cateChild .= $v2['cateChild'].',';
                }
                rtrim($cateChild,',');
            }

            $prochar = getDatas('prochar',['id'],['pro_id'=>$this->data['id']],'id desc');
            foreach ($prochar as $k_feture => $v_feture) {
                $featureSte = '';
                $feature = $this->getFeature($this->data['id'],$v_feture['id'],$this->lang);
                foreach ($feature['feature_value'] as $key => $value) {
                    $featureSte .= $v_feture['id'].'/'.$value.'/';
                }
                $checkRead['prochar'][$k_feture]['prochar_id'] = $v_feture['id'];
                $checkRead['prochar'][$k_feture]['prochar_title'] = mb_substr($featureSte,0,-1,'utf-8');
            }
            // dump($prochar);die;

            /**************详情页概要*****************/
            if($this->data['request'] == 'detail'){

                $checkRead_roleWhere['status'] = ['in', '1,3,4'];
                $checkRead_roleWhere['pro_id'] = $this->data['id'];
                $checkRead_role = getDatas('prochar',['*'],$checkRead_roleWhere,'id desc');
                // dump($checkRead_role);die;
                if(!empty($checkRead_role)){
                    foreach ($checkRead_role as $k3 => $v3) {
                        $checkRead_role[$k3]['create_name'] =  getinfo($v3['create_id'], 'admin')['nickname'];//编辑者
                        $checkRead_role[$k3]['create_at'] = getTimeDate($v3['create_at'],9);
                        $feature = $this->getFeature($v3['pro_id'],$v3['id'],$this->lang);
                        $checkRead_role[$k3]['feature_value'] = $feature['feature_value'];
                        $checkRead_role[$k3]['note_content'] = getValue('multi_lang',['mkey'=>$v3['ctn_note'],'lang'=>$this->lang],'content');
                        $feature_name = $feature['feature_name'];
                        $checkRead_role[$k3]['status_name'] = $v3['status'] == 1 ? lang('normal') : ($v3['status'] == 2 ? lang('delete') : ($v3['status'] == 3 ? lang('hide') : lang('off_the_shelf')));
                    }
                }else{
                    $feature_name = [];
                }
                
                $returnData['checkRead'] = $checkRead;
                $returnData['checkRead_role'] = $checkRead_role;
                // dump($returnData);die;
                return view('detail',['title'=>'summary','id'=>$this->data['id'],'checkRead'=>$checkRead,'checkRead_role'=>$checkRead_role,'feature_name'=>$feature_name]);

            /**************详情页介绍*****************/
            }else if($this->data['request'] == 'detailintro'){
                // 获取所有介绍类型名称
                $prodesc_cat_title = getSearch('prodesc_cat','zu',['zu.id,mu.content'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id asc');
                // dump($prodesc_cat_title);die;
                
                if(!empty($prodesc_cat_title)){
                    // 自定义获取介绍类型
                    $prodesc_cat_id = $prodesc_cat_title[0]['id'];
                    // 判断是否指定获取某一项介绍类型
                    if(isset($this->data['prodesc_cat_id'])) $prodesc_cat_id = $this->data['prodesc_cat_id'];
                    foreach ($prodesc_cat_title as $k2 => $v2) {
                        if($v2['id'] == $prodesc_cat_id){
                            $prodesc_cat_title[$k2]['active'] = true;
                            $prodesc_catname = $v2['content'];
                        }
                    }
                    $prodesc_cat_content = getDatas('prodesc',['id,lang,content'],['pro_id'=>$this->data['id'],'prodesc_cat_id'=>$prodesc_cat_id,'status'=>1],'id desc');
                    // dump($prodesc_cat_content);die;
                    if(!empty($prodesc_cat_content)){
                        foreach ($prodesc_cat_content as $k1 => $v1) {
                            $prodesc_cat_content[$k1]['title'] = $prodesc_catname.'-'.getNoticLang($v1['lang']);
                            $prodesc_cat_content[$k1]['section'] = 'section-'.$v1['lang'];   
                        }
                    }
                    return view('detailintro',['title'=>'information','id'=>$this->data['id'],'checkRead'=>$checkRead,'prodesc_cat_title'=>$prodesc_cat_title,'prodesc_cat_content'=>$prodesc_cat_content,'prodesc_cat_id'=>$prodesc_cat_id]);
                }else{
                    return view('detailintro',['title'=>'information','id'=>$this->data['id'],'checkRead'=>$checkRead]);
                }
                
                
                
                
                

            /**************详情页配件*****************/
            }else if($this->data['request'] == 'detailitem'){
                 // 获取角色和角色特征信息
                $procharlist = $this->getItemChar(['id,model,ctn_note'],['pro_id'=>$this->data['id']]);
                if (isset($this->data['prochar_id']) && $this->data['prochar_id']) {
                    $prochar_id = $this->data['prochar_id'];
                }else{
                    $prochar_id = $procharlist[0]['id'];
                }

                $itemSet = getDatas('pro_item',['*'],['pro_id'=>$this->data['id'],'prochar_id'=>$prochar_id]);
                // dump($procharlist);die;
                foreach ($itemSet as $k => $v) {
                    $itemSet[$k]['title'] = '';
                    $prochar = getDatas('prochar',['*'],['id'=>$v['prochar_id2item']])[0];
                    $itemSet[$k]['title'] .= $v['id'].'/'.$prochar['model'];
                    $itemSet[$k]['ctn_size'] = $prochar['ctn_size'];
                    $itemSet[$k]['create_name'] = getinfo($prochar['create_id'], 'admin')['nickname'];//编辑者
                    $itemSet[$k]['create_at'] = getTimeDates($prochar['create_at'],9,$this->lang);
                    $itemSet[$k]['status'] = getViewMultiLanguage($prochar['status']);
                    $itemSet[$k]['content'] = getValue('multi_lang',['mkey'=>'pro_'.$v['pro_id2item'],'lang'=>$this->lang],'content');
                }
                // dump($itemSet);die; 
                return view('detailitem',['title'=>'accessory','id'=>$this->data['id'],'checkRead'=>$checkRead,'itemSet'=>$itemSet]);

            /**************详情页图片*****************/
            }else if($this->data['request'] == 'detailimg'){
                $imglist = getDatas('pro_img',['*'],['pro_id'=>$this->data['id'],'is_icon'=>2]);
                foreach ($imglist as $k => $v) {

                    $imglist[$k]['name'] = getValue('multi_lang',['mkey'=>'pro_'.$this->data['id'],'lang'=>$this->lang],['content']);
                    if($v['prochar_id'] > 0){
                        $imglist[$k]['name'] = $imglist[$k]['name'].' '.lang('model').'：'.getValue('prochar',['id'=>$v['prochar_id']],['model']);
                    }
                    
                }
                return view('detailimg',['title'=>'image','id'=>$this->data['id'],'checkRead'=>$checkRead,'imglist'=>$imglist]);

            /**************详情页价格*****************/
            }else if($this->data['request'] == 'detailprice'){
                return view('detailprice',['title'=>'price','id'=>$this->data['id'],'checkRead'=>$checkRead]);

            /**************详情页库存*****************/
            }else if($this->data['request'] == 'detailstore'){
                return view('detailstore',['title'=>'stock','id'=>$this->data['id'],'checkRead'=>$checkRead]);

            /**************详情页采购统计*****************/
            }else if($this->data['request'] == 'detailbuy'){
                return view('detailbuy',['title'=>'purchase_statistics','id'=>$this->data['id'],'checkRead'=>$checkRead]);

            /**************详情页销售统计*****************/
            }else if($this->data['request'] == 'detailsale'){
                return view('detailsale',['title'=>'sell_statistics','id'=>$this->data['id'],'checkRead'=>$checkRead]);
            }else{
                return '搞事情';
            }
        } 
    }

    public function detailCopy(Request $request){
        if($this->data['request'] == 'ajax'){
            if(isset($this->data['id']) && $this->data['id']){
                $prochar = getSearch('prochar','zu',['zu.*,mu.content ctn_note'],['zu.id'=>$this->data['id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id asc')[0];
                $featureArr = getDatas('profeature2char',['*'],['pro_id'=>$prochar['pro_id'],'prochar_id'=>$this->data['id']]);
                // dump($featureArr);die;
                if(!empty($featureArr)){
                    foreach ($featureArr as $k => $v) {
                        $prochar['feature'][$k]['name_id'] = $v['profeature_id'];
                        $prochar['feature'][$k]['name'] = getValue('multi_lang',['mkey'=>'proattr_'.$v['profeature_id'],'lang'=>$this->lang],'content');
                        $prochar['feature'][$k]['value_id'] = $v['profeature_value_id'];
                        $prochar['feature'][$k]['value'] = getValue('multi_lang',['mkey'=>'proattr_value_'.$v['profeature_value_id'],'lang'=>$this->lang],'content');
                    }
                }
            }
            // dump($prochar);die;
            return $prochar;
        }else{
            return view('detailcopy',['id'=>$this->data['id']]);
        }
    }

    public function detailCopyInsert(){
        if(!$this->data['pro_id']) return json_encode(array('status'=>2,'msg'=>'缺少提交数据'));
        if($this->data['ctn_note'] == '') return json_encode(array('status'=>2,'msg'=>'请输入角色说明'));
        if(hasData('prochar',['id'=>$this->data['id'],'model'=>$this->data['model']])) return json_encode(array('status'=>3,'msg'=>lang('model').'：'.$this->data['model'].' '.lang('already_exists')));
        if($this->data['codebar'] != ''){
            if(hasData('prochar',['id'=>$this->data['id'],'codebar'=>$this->data['codebar']])) return json_encode(array('status'=>3,'msg'=>lang('model').'：'.$this->data['codebar'].' '.lang('already_exists')));
        }
        $prochar['model']       = $this->data['model'];
        $prochar['codebar']     = $this->data['codebar'];
        $prochar['ctn_kgs']     = $this->data['ctn_kgs'];
        $prochar['ctn_m3']      = $this->data['ctn_m3'];
        $prochar['ctn_size']    = $this->data['ctn_size'];
        $prochar['package_qty'] = $this->data['package_qty'];
        $prochar['qty']         = $this->data['qty'];
        $prochar['status']      = $this->data['status'];
        $prochar['new_day']     = $this->data['new_day'];
        $prochar['pulloff_day'] = $this->data['pulloff_day'];
        $prochar['recom_day']   = $this->data['recom_day'];
        $prochar['create_id']   = $this->data['create_id'];
        $prochar['create_at']  = $this->data['create_at'];
        $prochar['pro_id']      = $this->data['pro_id'];
        $prochar_id = insertData('prochar',$prochar);
        if(!$prochar_id) return json_encode(array('status'=>0,'msg'=>lang('model').'：'.$this->data['model'].' '.lang('role_copied_failed')));
        // 角色说明
        $lang_cate = getLanguage();
        foreach ($lang_cate as $k => $v) {
            $res = insertData('multi_lang',['mkey'=>'prochar_note_'.$prochar_id,'lang'=>$v['code'],'content'=>$this->data['ctn_note']]);
            if($res) updateData('prochar',['id'=>$prochar_id],['ctn_note'=>'prochar_note_'.$prochar_id]);
        }
        // 产品角色特征
        if(isset($this->data['feature']) && !empty($this->data['feature'])){
            foreach ($this->data['feature'] as $k1 => $v1) {
                $insertRes = insertData('profeature2char',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$prochar_id,'profeature_id'=>$v1['name_id'],'profeature_value_id'=>$v1['value_id']]);
            }
        }

        // 获取被复制角色的介绍类型内容
        $prochar_desc = getDatas('prodesc',['*'],['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['id']]);
        if(!empty($prochar_desc)){
            foreach ($prochar_desc as $k2 => $v2) {
                unset($v['id']);
                $v2['create_id'] = $this->user_id;
                $v2['create_at']= time();
                $v2['update_id'] = 0;
                $v2['update_at']= time();
                $v2['prochar_id']= $prochar_id;
                $res = insertData('prodesc',$v2);
            }
        }

        return json_encode(array('status'=>1,'msg'=>lang('model').'：'.$this->data['model'].' '.lang('role_copied_successfully')));

    }

    public function productEdit(Request $request)
    {
        // return $this->data;
        $join = [['multi_lang mu', 'zu.mkey = mu.mkey']];
        if($this->data['request'] == 'edit'){
            if($this->data['id']){
                $proinfo = getMultiLangName('pro',['*'],['id'=>$this->data['id']],'id desc')[0];
                // 当前产品简介
                $desc = getDatas('prodesc',['id,lang,content'],['pro_id'=>$proinfo['id'],'prodesc_cat_id'=>1],'id desc');
                foreach ($desc as $k => $v) {
                    $proinfo['prodesc_'.$v['lang']] = $v['content'];
                    // $lang[] = $v['lang'];
                }
                // dump($desc);die;
                // 当前产品分类
                $cateArr = getDatas('pro2cate',['procat_id'],['pro_id'=>$proinfo['id']]);
                if(!empty($cateArr)){
                    foreach ($cateArr as $k1 => $v1) {
                        $proinfo['procate'][$k1] = $v1['procat_id'];
                    }
                }else{
                    $proinfo['procate'] = [];
                }
                
                $returnData['procate'] = $proinfo['procate'];

                // 产品绑定产品角色
                $prochar = getMultiLangName('prochar',['*'],['pro_id'=>$proinfo['id']],'id desc');
                // dump($prochar);die;
                // 获取每个产品角色的所有特征
                if(!empty($prochar)){
                    $lang = Db::name('language')->field(['code'])->select();
                    // dump($lang);die;
                    foreach ($lang as $key => $value) {
                        $lang[$key] = $value['code'];
                    }
                    foreach ($prochar as $k2 => $v2) {
                        foreach ($lang as $klang => $vlang) {
                            // 产品角色信息
                            $procharlist[$vlang][$k2]['id'] = $v2['id'];
                            $procharlist[$vlang][$k2]['model'] = $v2['model'];
                            $procharlist[$vlang][$k2]['package_qty'] = $v2['package_qty'];
                            $procharlist[$vlang][$k2]['qty'] = $v2['qty'];
                            $procharlist[$vlang][$k2]['ctn_size'] = $v2['ctn_size'];
                            $procharlist[$vlang][$k2]['ctn_kgs'] = $v2['ctn_kgs'];
                            $procharlist[$vlang][$k2]['ctn_note'] =  isset($v2['name_'.$vlang]) ? $v2['name_'.$vlang] : '';
                            $procharlist[$vlang][$k2]['codebar'] = $v2['codebar'];
                            $procharlist[$vlang][$k2]['factory_model'] = $v2['factory_model'];
                            $prochar_featureArr = getDatas('profeature2char',['profeature_id,profeature_value_id'],['prochar_id'=>$v2['id']],'id desc');

                            if(!empty($prochar_featureArr)){
                                foreach ($prochar_featureArr as $k3 => $v3) {
                                    $profeature_name = getMultiLangName('proattr',['*'],['mkey'=>'proattr_'.$v3['profeature_id']],'id desc')[0];
                                    $procharlist[$vlang][$k2]['symbol'][$k3]['id'] = $v3['profeature_id'];
                                    $procharlist[$vlang][$k2]['symbol'][$k3]['key'] = isset($profeature_name['name_'.$vlang]) == true ?$profeature_name['name_'.$vlang] : '' ;
                                    $procharlist[$vlang][$k2]['symbol'][$k3]['selval'] = $v3['profeature_value_id'];

                                    $procharlist[$vlang][$k2]['symbol'][$k3]['value'] = getSearch('proattr_value','zu',['zu.id val,mu.content name'],['zu.proattr_id'=>$v3['profeature_id'],'mu.lang'=>$vlang],$join,'zu.id desc');
                                }
                            }else{
                                $procharlist[$vlang][$k2]['symbol']=[];
                            }

                            // 产品详细信息
                            $proinfolist[$vlang]['id'] = $proinfo['id'];
                            $proinfolist[$vlang]['brand_id'] = $proinfo['brand_id'];
                            $proinfolist[$vlang]['is_sale'] = $proinfo['is_sale'];
                            $proinfolist[$vlang]['status'] = $proinfo['status'];
                            $proinfolist[$vlang]['ncm'] = $proinfo['ncm'];
                            $proinfolist[$vlang]['name'] = $proinfo['name_'.$vlang];
                            $proinfolist[$vlang]['prodesc'] = isset($proinfo['prodesc_'.$vlang]) ? $proinfo['prodesc_'.$vlang] : '';

                            // 分类信息
                            $whereCate['zu.status'] = ['in','1,3,4'];
                            $whereCate['mu.lang'] = $vlang;
                            $product_cate[$vlang] = getSearch('procat','zu',['zu.*,mu.lang,mu.content'],$whereCate,$join,'zu.drank asc');
                            // 所有品牌信息
                            // $product_brand_data[$vlang] = getSearch('pro_brand','zu',['zu.id,mu.content'],['zu.status'=>1,'mu.lang'=>$vlang],$join,'zu.drank asc');
                            $product_brand_data[$vlang] = getDatas('pro_brand',['id,name'],['status'=>1],'drank asc');
                            
                            // $product_cate_data[$vlang] =getTree($product_cate_data[$vlang]);
                            // 所有特征
                            $profeature[$vlang] = getSearch('proattr','zu',['zu.*,mu.content'],['mu.lang'=>$vlang],$join,'zu.drank asc');
                        }
                    }
                }else{
                    return json_encode( json_encode(array('status'=>2,'msg'=>lang('missing_role_product'))));
                }

                $product_cate_data['zh'] = getTree($product_cate['zh']);
                $product_cate_data['en'] = getTree($product_cate['en']);
                $product_cate_data['pt'] = getTree($product_cate['pt']);
                foreach ($product_cate_data as $key => $value) {
                    foreach ($value as $_k => $_v) {
                        if($key == $_v['lang']){
                            $cateData[$key][] = $_v;
                        }
                    }
                }
                // 所有特征
                $returnData['proinfo'] = $proinfolist;
                $returnData['prochar'] = $procharlist;
                $returnData['product_brand_data'] = $product_brand_data;
                $returnData['product_cate_data'] = $cateData;
                $returnData['profeature'] = $profeature;
                $returnData['title'] = lang('edit');
                // dump($returnData['product_cate_data']);die;
                if($request->isAjax()){
                    return json_encode($returnData);
                }else{
                    return view('addnew',['id'=>$this->data['id'],'type'=>'edit','title'=>lang('edit')]);
                }
            }else{
                // return json_encode(array('status'=>500,'msg'=>'缺少ID参数'));
            }
        }else if($this->data['request'] == 'update'){
            unset($this->data['request']);
            return $this->productUpdate($this->data);

        }
    }


    function productUpdate($data){
        // return $data;
        if(isset($data['cateval']) && isset($data['brandval']) ){
                $i=0;
                foreach ($data as $k => $v) {
                    if($k == 'zh' || $k == 'en' || $k == 'pt'){
                        if($v['proname'] == ''){
                            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
                        }else{
                            // 产品角色信息为数组结构
                            $product_role[$k] = $v['datvis'];
                            $pro_multi[$i]['content'] =  $v['proname'];// 产品名
                            $pro_multi[$i]['lang'] = $k;// 语言类型

                            $pro_desc[$i]['content'] = $v['proint'];// 产品简介
                            $pro_desc[$i]['lang'] = $k;// 语言类型

                            //产品
                            $i++;
                        }
                    }
                }
                
                // 产品待绑定分类
                $product_cate_array = $data['cateval'];

                // dump($product_cate_array);die;
                // // 遍历产品角色
                foreach ($product_role as $k1 => $v1) {
                    if(!empty($v1)){
                        foreach ($v1 as $k2 => $v2) {
                            // 产品角色特征
                            if(!empty($v2['symbol'])){
                                $feature[$k2] =  $v2['symbol'];
                                unset($v2['symbol']);
                            }else{
                                $feature = [];
                            }
                            // 产品角色说明
                            $ctn_note[$k1][$k2]['content'] = $v2['ctn_note'] == null ? '' : $v2['ctn_note'] ;
                            $ctn_note[$k1][$k2]['lang'] = $k1;
                            unset($v2['ctn_note']);
                            // 产品角色基本信息
                            $prochar_insert[$k2] = $v2;
                        }
                    }
                }
                foreach ($ctn_note as $key => $value) {
                    foreach ($value as $kn => $vn) {
                        $ctn_notearr[$kn][$key] = $vn;
                    }
                }
                // dump($prochar_insert);die;
                Db::startTrans();
                    //产品详情
                    $product['brand_id'] = $data['brandval'];
                    $product['is_sale'] = $data['isfei'];
                    // $product['is_public'] = $data['isgeneral'];
                    $product['status'] = $data['statue'];
                    $product['update_id'] = $this->user_id;
                    $product['update_at'] = time();
                    $pro_id = updateData('pro',['id'=>$data['id']],$product);
                    if($pro_id >= 0){
                        // 编辑 产品名 多语言
                        foreach ($pro_multi as $keypro_multi => $valuepro_multi) {
                            $pro_multi_update = updateData('multi_lang',['mkey'=>'pro_'.$data['id'],'lang'=>$valuepro_multi['lang']],$valuepro_multi);
                        }
                        // 编辑 产品描述 多语言
                        foreach ($pro_desc as $keypro_desc => $valuepro_desc) {
                            $valuepro_desc['update_id'] = $this->user_id;
                            $valuepro_desc['update_at'] = time();
                            $pro_desc_update = updateData('prodesc',['pro_id'=>$data['id'],'lang'=>$valuepro_desc['lang']],$valuepro_desc);
                        }
                        

                        $delprochar = getDatas('prochar',['*'],['pro_id'=>$this->data['id']]);
                        if(!empty($delprochar)){
                            foreach ($delprochar as $k_del => $v_del) {
                                // 删除产品角色说明 多语言
                                $res = deleteData('multi_lang',['mkey'=>$v_del['ctn_note']]);
                                // 删除产品下所有角色的绑定特征
                                $res1 = deleteData('profeature2char',['pro_id'=>$data['id']]);
                                // 删除产品下所有角色。
                                $res2 = deleteData('prochar',['pro_id'=>$data['id']]);
                            }
                        }


                        
                        //  新增产品角色
                        if($prochar_insert){
                            foreach ($prochar_insert as $k6 => $v6) {
                                unset($prochar_insert[$k6]['id']);
                                $prochar_insert[$k6]['pro_id'] = $data['id'];
                                $prochar_insert[$k6]['create_id'] = $this->user_id == null ? 1 : $this->user_id ;
                                $prochar_insert[$k6]['create_at'] = time();
                                $prochar_insert_id = insertData('prochar',$prochar_insert[$k6]);

                                if($prochar_insert_id){
                                    foreach ($ctn_notearr[$k6] as $k3 => $v3) {
                                        // 给产品角色说明增加多语言关联,并新增
                                        $ctn_noteinsert['mkey'] = 'prochar_note_'.$prochar_insert_id;
                                        $ctn_noteinsert['lang'] = $v3['lang'];;
                                        $ctn_noteinsert['content'] = $v3['content'];
                                        $ctn_note_insert = insertData('multi_lang',$ctn_noteinsert);
                                    }
                                    if(!empty($feature)){
                                        // 绑定产品角色特征
                                        foreach ($feature[$k6] as $k4 => $v4) {
                                            $profeature2charData['pro_id'] = $data['id'];
                                            $profeature2charData['prochar_id'] = $prochar_insert_id;
                                            $profeature2charData['profeature_id'] = $v4['id'];
                                            $profeature2charData['profeature_value_id'] = $v4['selval'];
                                            $procharFeatureInsert = insertData('profeature2char',$profeature2charData);
                                        }
                                    }
                                    // 更新产品角色的包装说明关联字段
                                    $prochar_update = updateData('prochar',['id'=>$prochar_insert_id],['ctn_note'=>'prochar_note_'.$prochar_insert_id]);
                                }
                            }
                            
                        }
                        
                        
                        // 编辑产品绑定产品分类,先把之前绑定数据删除，再新追加
                        $delRes = deleteData('pro2cate',['pro_id'=>$data['id']]);
                        if(!empty($product_cate_array)){
                            foreach ($product_cate_array as $k5 => $v5) {
                                $pro_bind_cate = insertData('pro2cate',['pro_id'=>$data['id'],'procat_id'=>$v5,'mkey'=>'procat_'.$v5]);
                            }
                        }
                        $content = getValue('multi_lang',['mkey'=>'pro_'.$data['id'],'lang'=>$this->lang],'content');  
                        if($pro_multi_update>=0 && $pro_desc_update>=0 && $prochar_update>=0 )
                        {
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>$content.' '.lang('edited_successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>$content.' '.lang('edit_failed')));
                        }
                    }else{
                        return json_encode(array('status'=>0,'msg'=>$content.' '.lang('edit_failed')));
                    }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
    }

    public function productAdd(){

        if(isset($this->data['request']) && $this->data['request'] == "add"){
            // 判断是否根据属性名称获取属性值
            if(isset($this->data['getcontent']) && $this->data['getcontent'] == 'feature'){
                return $this->getfeatureValue($this->data['id']);
            }
            $join = [
                        ['multi_lang mu', 'zu.mkey = mu.mkey']
                    ];
            $whereCate['zu.status'] = ['in','1,3,4'];
            $langArr = getLanguage($this->lang);
            foreach ($langArr as $klang => $vlang) {
                // 分类信息
                $whereCate['mu.lang'] = $vlang['code'];
                $procat = getSearch('procat','zu',['zu.id,zu.pid,mu.content'],$whereCate,$join,'zu.drank asc');
                $product_cate_data[$vlang['code']] =getTree($procat);
                // 品牌信息
                $product_brand_data[$vlang['code']] = getDatas('pro_brand',['id,name'],['status'=>1],'drank asc');
                // 特征
                $profeature[$vlang['code']] = getSearch('proattr','zu',['zu.id,mu.content'],['zu.status'=>1,'mu.lang'=>$vlang['code']],$join,'zu.drank asc');
                // $returnData['feature'] = getMultiLangName('proattr',['id,mkey'],'1=1','id desc');
            }
            $returnData['product_cate_data'] = $product_cate_data;
            $returnData['product_brand_data'] = $product_brand_data;
            $returnData['profeature'] = $profeature;
            $returnData['title'] = lang('add');
            return json_encode($returnData);
        }else if(isset($this->data['request']) && $this->data['request'] == "insert"){
            unset($this->data['request']);
            $res = $this->productInsert($this->data);
            return $res;
        }else{
            // return 123;
            return view('addnew',['type'=>'add']);
        }
    }


    function productInsert($data){
        // return $data;
        //判断产品绑定类和品牌是否有数据
        if(isset($data['cateval']) && isset($data['brandval']) ){
            if($data['zh']['datvis'][0]['model'] == ''){
                return json_encode(array('status'=>2,'msg'=>lang("product_role_can't_bem_empty")));
            }

            if($data['zh']['proname'] == ''){
                return json_encode(array('status'=>2,'msg'=>lang('missing_name')));
            }
            $lang_cate = getLanguage($this->lang);
            $i=0;
            foreach ($lang_cate as $k => $v) {
                // 产品角色信息为数组结构
                $product_role =  $data['zh']['datvis'];
                $pro_multi[$i]['content'] =  $data['zh']['proname'];// 产品名
                $pro_multi[$i]['lang'] = $v['code'];// 语言类型
                // if($v['code'] == $this->lang){
                $content = $data['zh']['proname'];
                // }

                $pro_desc[$i]['content'] = $data['zh']['proint'];// 产品简介
                $pro_desc[$i]['lang'] = $v['code'];// 语言类型
                $i++;

            }
            
            // 产品待绑定分类
            $product_cate_array = $data['cateval'];

            if(!empty($product_role[0]['symbol'])){
                foreach ($product_role as $k7 => $v7) {
                    foreach ($v7['symbol'] as $k8 => $v8) {
                        $feature[$k8] = $v8;
                    }
                    unset($product_role[$k7]['symbol']);
                }
            }else{
                $feature = [];
            }

            Db::startTrans();
            // try{
                //产品详情
                $product['brand_id']  = $data['brandval'];
                $product['ncm']       = $data['ncm'];
                $product['is_sale']   = $data['isfei'];
                // $product['is_public'] = $data['isgeneral'];
                $product['status']    = $data['statue'];
                $product['create_id'] = $this->user_id;
                $product['create_at']= time();
                $pro_id = insertData('pro',$product);

                if($pro_id){
                    // 设置一个系统默认图片
                    $proImgData['pro_id'] = $pro_id;
                    $proImgData['img_format'] = 'png';
                    $proImgData['img_url'] = "/static/Admin/img/nt_logo.png";
                    $proImgData['width'] = 1000;
                    $proImgData['height'] = 1000;
                    $proImgData['is_icon'] = 2;
                    $proImgData['drank'] = 0;
                    $proImgData['create_at'] = time();
                    $proImgData['create_id'] = $this->user_id;
                    $proImgData['create_at'] = time();
                    $imgRes = insertData('pro_img',$proImgData);

                    $pro_res = updateData('pro',['id'=>$pro_id],['mkey'=>'pro_'.$pro_id]);
                    // 添加 产品名 多语言
                    foreach ($pro_multi as $keypro_multi => $valuepro_multi) {
                        $valuepro_multi['mkey'] = 'pro_'.$pro_id;
                        $pro_multi_insert = insertData('multi_lang',$valuepro_multi);
                    }
                    // 添加 产品描述 多语言
                    foreach ($pro_desc as $keypro_desc => $valuepro_desc) {
                        $valuepro_desc['pro_id'] = $pro_id;
                        $valuepro_desc['create_id'] = $this->user_id;
                        $valuepro_desc['create_at'] = time();
                        $pro_desc_insert = insertData('prodesc',$valuepro_desc);
                    }
                    
                    //  新增产品角色
                    if($product_role){

                        foreach ($product_role as $k6 => $v6) {
                            $product_role[$k6]['pro_id'] = $pro_id;
                            $product_role[$k6]['create_id'] = $this->user_id == null ? 1 : $this->user_id ;
                            $product_role[$k6]['create_at'] = time();
                            $prochar_insert_id = insertData('prochar',$product_role[$k6]);

                            if($prochar_insert_id){
                                foreach ($lang_cate as $k3 => $v3) {
                                    // 给产品角色说明增加多语言关联,并新增
                                    $ctn_note['mkey'] = 'prochar_note_'.$prochar_insert_id;
                                    $ctn_note['lang'] = $v3['code'];
                                    $ctn_note['content'] = $product_role[$k6]['ctn_note'];
                                    $ctn_note_insert = insertData('multi_lang',$ctn_note);
                                }
                                if(!empty($feature)){
                                    // 绑定产品角色特征
                                    foreach ($feature as $k4 => $v4) {
                                        $profeature2charData['pro_id'] = $pro_id;
                                        $profeature2charData['prochar_id'] = $prochar_insert_id;
                                        $profeature2charData['profeature_id'] = $v4['id'];
                                        $profeature2charData['profeature_value_id'] = $v4['selval'];
                                        $procharFeatureInsert = insertData('profeature2char',$profeature2charData);
                                    }
                                }
                                // 更新产品角色的包装说明关联字段
                                $prochar_update = updateData('prochar',['id'=>$prochar_insert_id],['ctn_note'=>'prochar_note_'.$prochar_insert_id]);
                            }
                        }
                        
                    }
                    
                    
                    // 产品角色绑定产品分类
                    foreach ($product_cate_array as $k5 => $v5) {
                        $pro_bind_cate = insertData('pro2cate',['pro_id'=>$pro_id,'procat_id'=>$v5,'mkey'=>'procat_'.$v5]);
                    }

                    if($pro_multi_insert && $pro_desc_insert && $pro_bind_cate )
                    {
                        Db::commit();
                        return json_encode(array('status'=>1,'msg'=>$content." ".lang('added_successfully')));
                    }else{
                        Db::rollback();
                        return json_encode(array('status'=>0,'msg'=>$content." ".lang('add_failed')));
                    }
                }else{
                    Db::rollback();
                    return json_encode(array('status'=>0,'msg'=>$content." ".lang('add_failed')));
                }
            // }catch(\Exception $e){
            //     Db::rollback();
            //     return json_encode(array('status'=>500,'msg'=>lang('mysql_fail_1')));
            // }
        }else{
            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
        }
    }


    // 根据特征id，获取其子级
    public function getfeatureValue($id){
        if($id){
            $featureData = getMultiLangName('proattr_value',['*'],['proattr_id'=>$id],'id asc');
            $featureName = getMultiLangName('proattr',['*'],['id'=>$id],'id asc')[0];
            foreach ($featureData as $k => $v) {
                $featureData[$k]['feature_id'] = $featureName['id'];
                $featureData[$k]['featureName_zh'] = $featureName['name_zh'];
                $featureData[$k]['featureName_en'] = $featureName['name_en'];
                $featureData[$k]['featureName_pt'] = $featureName['name_pt']; 
            }
            return json_encode(array('status'=>1,'msg'=>lang('successfully'),'data'=>$featureData));
        }else{
            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
        }
    }

    // proaddnew 跳转
    public function proAddNewJump(Request $request)
    {   
        if(isset($this->data['pro_id'])){
            // 产品介绍
            if($this->data['request'] == 'intro'){
                $prodesc_cat_id = getDatas('prodesc',['id','prochar_id','prodesc_cat_id'],['pro_id'=>$this->data['pro_id'],'lang'=>$this->lang,'prodesc_cat_id'=>['neq',0]]);
                // $prodesc_cat_id = getGroup('prodesc',['id','prochar_id','prodesc_cat_id'],['pro_id'=>$this->data['pro_id'],'prodesc_cat_id'=>['neq',0]],'prodesc_cat_id');

                if(empty($prodesc_cat_id)) view('proaddnew_intro',['title'=>lang('information')]);
                // dump($prodesc_cat_id);die;
                if(!empty($prodesc_cat_id)){
                    foreach ($prodesc_cat_id as $k2 => $v2) {
                        $prodesc_cat[$k2] = getSearch('prodesc_cat','zu',['zu.*,mu.content'],['zu.id'=>$v2['prodesc_cat_id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                        $prodesc_cat[$k2]['prodesc_id'] = $v2['id'];
                        $prodesc_cat[$k2]['prochar_id'] = $v2['prochar_id'];
                        $prodesc_cat[$k2]['prochar_model'] = getValue('prochar',['id'=>$v2['prochar_id']],'model');
                    }
                }else{
                    $prodesc_cat = [];
                }
                // $prodesc_cat = 
                if(!empty($prodesc_cat)){
                    foreach ($prodesc_cat as $k => $v) {
                        $prodesc_cat[$k]['verify'] = $v['verify'] == 1 ? lang('need') : lang('no_need');
                        $prodesc_cat[$k]['create_name'] = getinfo($v['create_id'], 'admin')['nickname'];//编辑者
                        $prodesc_cat[$k]['created_time'] = getTimeDates($v['create_at'],9,$this->lang);
                        // 获取介绍类型得支持语言
                        $prodesc_cat[$k]['langStr'] = '';
                        if($v['lang_ids'] != ''){
                            $lang_idsArr = explode(',', $v['lang_ids']);
                            foreach ($lang_idsArr as $k1 => $v1) {
                                switch ($v1) {
                                    case 'zh':
                                        $prodesc_cat[$k]['langStr'] .=lang('Chinese').',';
                                        break;
                                    case 'en':
                                        $prodesc_cat[$k]['langStr'] .= lang('English').',';
                                        break;
                                    case 'pt':
                                        $prodesc_cat[$k]['langStr'] .= lang('PORTUGUES').',';
                                        break;
                                    default:
                                        $prodesc_cat[$k]['langStr'] .= lang('others').',';
                                        break;
                                }
                            }
                            $prodesc_cat[$k]['langStr'] = rtrim($prodesc_cat[$k]['langStr'],',');
                        }
                    }
                }

                $returnData['size'] = count( $prodesc_cat);
                if(isset($this->data['page']) && isset($this->data['limit'])){
                    $prodesc_cat = array_slice($prodesc_cat,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                    $returnData['page'] = $this->data['page'];
                }else{
                    $returnData['page'] =1;
                }

                if($request->isAjax()){
                    $returnData['prodesc_cat'] = $prodesc_cat;
                    $returnData['title'] = lang('information');
                    $returnData['pro_id'] = $this->data['pro_id'];
                    return json_encode($returnData);
                }else{
                    return view('proaddnew_intro',['title'=>lang('information'),'prodesc_cat'=>$prodesc_cat,'pro_id'=>$this->data['pro_id']]);
                }
                
            }else if($this->data['request'] == 'item'){ // 产品配件
                if($request->isAjax()){
                    // return $this->data;
                    // 获取角色和角色特征信息
                    $procharlist = $this->getItemChar(['id,model,ctn_note'],['pro_id'=>$this->data['pro_id']]);
                    // dump($procharlist);die;
                    if(empty($procharlist)) return json_encode(array('status'=>2,'msg'=>lang("product_role_can't_bem_empty")));
                    if (isset($this->data['prochar_id']) && $this->data['prochar_id']) {
                        $prochar_id = $this->data['prochar_id'];
                    }else{
                        $prochar_id = $procharlist[0]['id'];
                    }

                    $itemSet = getDatas('pro_item',['*'],['pro_id'=>$this->data['pro_id'],'prochar_id'=>$prochar_id]);
                    $join=[
                                ['pro p','zu.pro_id = p.id'],
                                ['multi_lang pm','p.mkey = pm.mkey'],
                                ['pro_brand b','p.brand_id = b.id'],
                                ['pro_img i','p.id = i.pro_id']
                            ];
                    $field = ['zu.model,pm.content pro_name,b.name brand_name,i.img_url'];
                    $where = [
                                'zu.id'=>$prochar_id,
                            ];

                    $proInfo = getSearch('prochar','zu',$field,$where,$join,'zu.id desc')[0];
                    // return $proInfo;
                    foreach ($itemSet as $k => $v) {
                        $itemSet[$k]['title'] = '';
                        $prochar = getDatas('prochar',['*'],['id'=>$v['prochar_id2item']])[0];
                        // dump($prochar);die;
                        $itemSet[$k]['title'] .= $v['id'].'/'.$proInfo['brand_name'].'/'.$proInfo['pro_name'].'/'.$proInfo['model'];
                        $itemSet[$k]['proimg_url'] = $proInfo['img_url'];
                        $itemSet[$k]['ctn_size'] = $prochar['ctn_size'];
                        $itemSet[$k]['ctn_size'] = $prochar['ctn_size'];
                        $itemSet[$k]['create_name'] = getinfo($prochar['create_id'], 'admin')['nickname'];//编辑者
                        $itemSet[$k]['create_at'] = getTimeDates($prochar['create_at'],9,$this->lang);
                        $itemSet[$k]['status'] = lang($prochar['status']);
                        $itemSet[$k]['content'] = getValue('multi_lang',['mkey'=>'pro_'.$v['pro_id2item'],'lang'=>$this->lang],'content');
                    }
                    $returnData['size'] = count( $itemSet);
                    if(isset($this->data['page']) && isset($this->data['limit'])){
                        $itemSet = array_slice($itemSet,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                        $returnData['page'] = $this->data['page'];
                    }else{
                        $returnData['page'] =1;
                    }
                    $returnData['itemSet'] = $itemSet;
                    $returnData['pro_id'] = $this->data['pro_id'];
                    $returnData['procharlist'] = $procharlist;
                    return json_encode($returnData);
                }else{
                    return view('proaddnew_item',['title'=>lang('accessory'),'pro_id'=>$this->data['pro_id']]);
                }
                
            }else if($this->data['request'] == 'img'){
                $imglist = getDatas('pro_img',['*'],['pro_id'=>$this->data['pro_id'],'width'=>1000]);
                foreach ($imglist as $k => $v) {

                    $imglist[$k]['name'] = getValue('multi_lang',['mkey'=>'pro_'.$this->data['pro_id'],'lang'=>$this->lang],['content']);
                    if($v['prochar_id'] > 0){
                        $imglist[$k]['name'] = $imglist[$k]['name'].' '.lang('model').'：'.getValue('prochar',['id'=>$v['prochar_id']],['model']);
                    }
                }
                // dump($imglist);die;
                if($request->isAjax()){
                    $returnData['title'] = lang('image');
                    $returnData['pro_id'] = $this->data['pro_id'];
                    $returnData['imglist'] = $imglist;
                    return json_encode($returnData);
                }else{
                    return view('proaddnew_img',['title'=>lang('image'),'pro_id'=>$this->data['pro_id'],'imglist'=>$imglist]);
                }
                
            }
        }
    }
    //  产品介绍添加
    public function proAddNewJump_introAdd()
    {
        // return $this->data;
        if(isset($this->data['request']) && $this->data['request'] == "insert"){
            // return $this->data;
            if(isset($this->data['id']) && $this->data['id']){
                if(isset($this->data['lang']) && $this->data['lang']){
                    if(isset($this->data['content']) && $this->data['content']){
                        // 判断当前产品是否有介绍类型内容
                        $ishas = hasData('prodesc',['pro_id'=>$this->data['pro_id'],'prodesc_cat_id'=>$this->data['id'],'prochar_id'=>$this->data['prochar_id'],'lang'=>$this->data['lang']]);
                        $proname = getValue('multi_lang',['mkey'=>'pro_'.$this->data['pro_id'],'lang'=>$this->lang],'content');
                        $prodesc_catname = getValue('multi_lang',['mkey'=>'prodesc_cat_'.$this->data['id'],'lang'=>$this->lang],'content');
                        if(!$ishas){
                            // 根据Id获取介绍类型的详情
                            $prodesc_cat = getinfo($this->data['id'],'prodesc_cat');
                            // return $prodesc_cat;
                            if($prodesc_cat['lang_ids'] != ''){
                                $verify = $prodesc_cat['verify'] == 1 ? 1 :2;
                                $lang_idsArr = explode(',', $prodesc_cat['lang_ids']);
                                // 添加时该介绍类型的所有支持语言都新增同一信息。
                                foreach ($lang_idsArr as $k => $v) {
                                    $res = insertData('prodesc',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'prodesc_cat_id'=>$this->data['id'],'lang'=>$v,'content'=>$this->data['content'],'create_id'=>$this->user_id,'create_at'=>time(),'verify'=>$verify]);
                                }
                                if($res){
                                    return json_encode(array('status'=>1,'msg'=>$proname.'->'.$prodesc_catname.' '.lang('added_successfully')));
                                }else{
                                    return json_encode(array('status'=>0,'msg'=>$proname.'->'.$prodesc_catname.' '.lang('add_failed')));
                                }
                            }
                        }else{
                            return json_encode(array('status'=>3,'msg'=>'当前产品已经拥有'.$this->data['lang'].'语言下的介绍类型'));
                        }
                        
                    }else{
                        return json_encode(array('status'=>2,'msg'=>lang('missing_content')));
                    }
                }else{
                    return json_encode(array('status'=>2,'msg'=>lang('missing').' '.lang('language')));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
        }else if(isset($this->data['request']) && $this->data['request'] == "add"){
            $prodesc_cat = getSearch('prodesc_cat','zu',['zu.*,mu.content'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
            // view 渲染第一个数据的支持语言，防止只有一个支持语言在view触发不了change事件
            if($prodesc_cat[0]['lang_ids'] != ''){
                $lang_idsArr = $this->getLangIndorsement($prodesc_cat[0]['lang_ids']);
            }

            $prochar = getDatas('prochar',['id','model'],['pro_id'=>$this->data['pro_id']],'id desc');
            foreach ($prochar as $k_feture => $v_feture) {
                $featureSte = '';
                $feature = $this->getFeature($this->data['pro_id'],$v_feture['id'],$this->lang);
                foreach ($feature['feature_value'] as $key => $value) {
                    $featureSte .= $v_feture['id'].'/'.$v_feture['model'].'/'.$value.'/';
                }
                $prochar[$k_feture]['prochar_title'] = mb_substr($featureSte,0,-1,'utf-8');
            }
            return view('intraddopen',['title'=>lang("product_introduction_type_added"),'prodesc_cat'=>$prodesc_cat,'lang_idsArr'=>$lang_idsArr,'pro_id'=>$this->data['pro_id'],'request'=>'add','prochar'=>$prochar]);
        }else{
            return $this->proAddNewJump_introAjax($this->data,$this->lang);
        }
    }

    public function proAddNewJump_introDel()
    {
        // return $this->data;
        if(isset($this->data['prodesc_cat_id']) && isset($this->data['pro_id'])){
            $content = getValue('multi_lang',['mkey'=>'prodesc_cat_'.$this->data['prodesc_cat_id'],'lang'=>$this->lang],'content');
            $res = deleteData('prodesc',['prodesc_cat_id'=>$this->data['prodesc_cat_id'],'pro_id'=>$this->data['pro_id']]);
            if($res){
                return json_encode(array('status'=>1,'msg'=>$content.' '.lang('cleared_successfully')));
            }else{
                return json_encode(array('status'=>0,'msg'=>$content.' '.lang('clear_failed')));
            }
        }
    }

    // 获取支持的多语言信息
    function getLangIndorsement($lang_ids){
        $lang_ids = explode(',', $lang_ids);
        foreach ($lang_ids as $k => $v) {
            switch ($v) {
                case 'zh':
                    $lang_idsArr[$k]['lang'] =$v;
                    $lang_idsArr[$k]['content'] =lang('Chinese');
                    break;
                case 'en':
                    $lang_idsArr[$k]['lang'] =$v;
                    $lang_idsArr[$k]['content'] = lang('ENGLISH');
                    break;
                case 'pt':
                    $lang_idsArr[$k]['lang'] =$v;
                    $lang_idsArr[$k]['content'] = lang('PORTUGUES');
                    break;
                default:
                    $lang_idsArr[$k]['lang'] ='qt';
                    $lang_idsArr[$k]['content'] = lang('others').',';
                    break;
            }
        }
        return $lang_idsArr;
    }


    // 产品介绍编辑
    public function proAddNewJump_introEdit()
    {
        if(isset($this->data['id']) && $this->data['id']){
            if(isset($this->data['request']) && $this->data['request'] == "update"){
                // return $this->data;
                if(isset($this->data['lang']) && $this->data['lang']){
                    if(isset($this->data['content']) && $this->data['content']){
                        // 同一个prodesc_cat_id 的 同一种lang 会有多个数据，但是status==1的同一时间只有一个数据，所有编辑修改的时候只修改status==1 的数据
                        $res = updateData('prodesc',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'prodesc_cat_id'=>$this->data['id'],'lang'=>$this->lang,'status'=>1],['content'=>$this->data['content'],'update_id'=>$this->user_id,'create_at'=>time()]);
                        $proname = getValue('multi_lang',['mkey'=>'pro_'.$this->data['pro_id'],'lang'=>$this->lang],'content');
                        $prodesc_catname = getValue('multi_lang',['mkey'=>'prodesc_cat_'.$this->data['id'],'lang'=>$this->lang],'content');
                        if($res >=0){
                            return json_encode(array('status'=>1,'msg'=>$proname.'->'.$prodesc_catname.lang('edit').' '.lang('success')));
                        }else{
                            return json_encode(array('status'=>0,'msg'=>$proname.'->'.$prodesc_catname.lang('edit').' '.lang('fail')));
                        }
                    }else{
                        return json_encode(array('status'=>2,'msg'=>lang('missing_content')));
                    }
                }else{
                    return json_encode(array('status'=>3,'msg'=>lang('select_language')));
                }
            }else if(isset($this->data['request']) && $this->data['request'] == "edit"){
                // dump($this->data);die;
                $prodesc_cat = getSearch('prodesc_cat','zu',['zu.*,mu.content'],['zu.id'=>$this->data['id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');

                if($prodesc_cat[0]['lang_ids'] != ''){
                    $lang_idsArr = $this->getLangIndorsement($prodesc_cat[0]['lang_ids']);
                    $wherelang_ids = explode(',', $prodesc_cat[0]['lang_ids']);
                    $where['lang'] = $wherelang_ids[0];
                    $where['status'] = 1;
                    $where['prodesc_cat_id'] = $this->data['id'];
                    $where['pro_id'] = $this->data['pro_id'];
                    // 判断是否编辑某个角色得介绍类型内容
                    if(isset($this->data['prochar_id']) && $this->data['prochar_id']) $where['prochar_id'] = $this->data['prochar_id'];
                    $content = getValue('prodesc',$where,'content');
                }
                $prochar = getDatas('prochar',['id','model'],['pro_id'=>$this->data['pro_id']],'id desc');
                foreach ($prochar as $k_feture => $v_feture) {
                    $featureSte = '';
                    $feature = $this->getFeature($this->data['pro_id'],$v_feture['id'],$this->lang);
                    foreach ($feature['feature_value'] as $key => $value) {
                        $featureSte .= $v_feture['id'].'/'.$v_feture['model'].'/'.$value.'/';
                    }
                    $prochar[$k_feture]['prochar_title'] = mb_substr($featureSte,0,-1,'utf-8');
                }
                // dump($content);die;
                return view('intraddopen',['title'=>lang('edit_product_introduction_type'),'prodesc_cat'=>$prodesc_cat,'lang_idsArr'=>$lang_idsArr,'content'=>$content,'pro_id'=>$this->data['pro_id'],'request'=>'edit','prochar'=>$prochar,'prochar_id'=>$this->data['prochar_id']]);
            }else{
                return $this->proAddNewJump_introAjax($this->data,$this->lang);
            }
        }else{
            return array('status'=>3,'msg'=>lang('no_data'));
        }
    }

    // 产品介绍AJAX
    public function proAddNewJump_introAjax($data,$lang)
    {
        if($data['id']){
            if(isset($data['lang']) && $data['lang']){
                $content['prodesc_cat_id'] = $data['id'];
                $where['prodesc_cat_id'] = $data['id'];
                $where['lang'] = $lang;
                $where['pro_id'] = $data['pro_id'];
                if(isset($data['prochar_id']) && $data['prochar_id'] > 0) $where['prochar_id'] = $this->data['prochar_id'];
                $content['content'] = getValue('prodesc',$where,'content');
                return json_encode(array('status'=>1,'msg'=>'OK','data'=>$content));
            }else{
                $lang_ids = getValue('prodesc_cat',['id'=>$data['id']],'lang_ids');
                if($lang_ids != ''){
                    $lang_idsArr = $this->getLangIndorsement($lang_ids);
                    return json_encode(array('status'=>1,'msg'=>lang('successfully'),'data'=>$lang_idsArr));
                }else{
                    return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data'),'data'=>[]));
                }
            }
            
        }else{
            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
        }
    }


    //产品配件AJAX
    // public function proAddNewJump_itemAjax(){
    //     if($this->data['request'] == 'pro'){
    //         $pro2cate = getDatas('pro2cate',['*'],['procat_id'=>$this->data['cat_id']]);
    //         // return $pro2cate;
    //         if(!empty($pro2cate)){
    //             foreach ($pro2cate as $k => $v) {
    //                 if(isset($this->data['pro_id']) && $v['pro_id'] != $this->data['pro_id']){
    //                     $prolist[] = getSearch('pro','zu',['zu.id,mu.content'],['mu.lang'=>$this->lang,'zu.id'=>$v['pro_id']],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
    //                 }else{
    //                     $prolist[] = getSearch('pro','zu',['zu.id,mu.content'],['mu.lang'=>$this->lang,'zu.id'=>$v['pro_id']],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
    //                     // $prolist[] = getMultiLangName('pro',['*'],['id'=>$v['pro_id']],'id asc')[0];
    //                 }
    //             }
    //             return json_encode(array('status'=>2000,'msg'=>'OK','data'=>$prolist));
    //         }
    //     }elseif ($this->data['request'] == 'prochar') {
    //         $prochar = getDatas('prochar',['*'],['pro_id'=>$this->data['id']]);
    //         // return $prochar;
    //         if(!empty($prochar)){
    //             foreach ($prochar as $k => $v) {
    //                 $returnFeature[$k]['id'] =  $v['id'];
    //                 $returnFeature[$k]['content'] = lang('model').''.$v['model'].'/';
    //                 $prochar2feature = getDatas('profeature2char',['*'],['prochar_id'=>$v['id']]);
    //                 if(!empty($prochar2feature)){
    //                     foreach ($prochar2feature as $k1 => $v1) {
    //                         $feature = getSearch('proattr_value','zu',['zu.id,mu.content'],['mu.lang'=>$this->lang,'zu.id'=>$v1['profeature_value_id']],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
    //                         $returnFeature[$k]['content'] .= $feature['content'].'/';
    //                     }
    //                 }
    //             }
    //         }else{
    //             $returnFeature=[];
    //         }
    //         return json_encode(array('status'=>2000,'msg'=>'OK','data'=>$returnFeature));

    //     }else if($this->data['request'] == 'search'){
    //         // return $this->data;
    //         if(isset($this->data['searchText']) && $this->data['searchText']){
    //             $join  = [['multi_lang mu','zu.mkey = mu.mkey']];
    //             $field = ['zu.*,mu.content'];
    //             $where['mu.lang'] = $this->lang;
    //             if(isset($this->data['pro_id'])){
    //                 $where['zu.id'] = ['net',$this->data['pro_id']];
    //             }
                
    //             // 查找
    //             $where['zu.id|mu.content'] = ['like',"%".$this->data['searchText'].'%'];   
    //             $productList = getSearch('pro','zu',$field,['mu.lang'=>$this->lang,'zu.id'=>['neq',$this->data['pro_id']],'is_item'=>1],$join,'zu.id desc');
    //             return $productList;

    //         }else{
    //             return array();
    //         }
    //     }
    // }

    public function proAddNewJump_itemAjax($data,$lang){
        if($data['request'] == 'pro'){
            $pro2cate = getDatas('pro2cate',['*'],['procat_id'=>$data['cat_id']]);
            // return $pro2cate;
            if(!empty($pro2cate)){
                foreach ($pro2cate as $k => $v) {
                    if(isset($data['pro_id']) && $v['pro_id'] != $data['pro_id']){
                        $prolist[] = getSearch('pro','zu',['zu.id,mu.content'],['mu.lang'=>$lang,'zu.id'=>$v['pro_id'],'zu.is_item'=>0],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                    }else{
                        $prolist[] = getSearch('pro','zu',['zu.id,mu.content'],['mu.lang'=>$lang,'zu.id'=>$v['pro_id'],'zu.is_item'=>0],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                        // $prolist[] = getMultiLangName('pro',['*'],['id'=>$v['pro_id']],'id asc')[0];
                    }
                }
                return json_encode(array('status'=>1,'msg'=>'OK','data'=>$prolist));
            }else{
                return json_encode(array('status'=>3,'msg'=>lang('no_data')));
            }
        }elseif ($data['request'] == 'prochar') {
            $prochar = getDatas('prochar',['*'],['pro_id'=>$data['id']]);
            // return $prochar;
            if(!empty($prochar)){
                foreach ($prochar as $k => $v) {
                    $returnFeature[$k]['id'] =  $v['id'];
                    $returnFeature[$k]['content'] = lang('model').''.$v['model'].'/';
                    $prochar2feature = getDatas('profeature2char',['*'],['prochar_id'=>$v['id']]);
                    if(!empty($prochar2feature)){
                        foreach ($prochar2feature as $k1 => $v1) {
                            $feature = getSearch('proattr_value','zu',['zu.id,mu.content'],['mu.lang'=>$lang,'zu.id'=>$v1['profeature_value_id'],'zu.is_item'=>0],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                            $returnFeature[$k]['content'] .= $feature['content'].'/';
                        }
                    }
                }
                return json_encode(array('status'=>1,'msg'=>'OK','data'=>$returnFeature));
            }else{
                return json_encode(array('status'=>3,'msg'=>lang('no_data')));
            }

        }else if($data['request'] == 'searchText'){
            // return $this->data;
            if(isset($data['searchText']) && $data['searchText']){
                $join  = [['multi_lang mu','zu.mkey = mu.mkey']];
                $field = ['zu.*,mu.content'];
                $where['mu.lang'] = $lang;
                if(!isset($data['pro_id'])){
                    $data['pro_id'] = 0;
                }
                
                $where['zu.id'] = ['not in',$data['pro_id']];
                // 查找
                $where['zu.id|mu.content'] = ['like',"%".$data['searchText'].'%'];   
                $where['zu.is_item'] = 0;
                $where['zu.is_public'] = 1;
                $productList = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
                // dump($productList);die;
            }else{
                $productList = [];
            }
            if(!empty($productList)){
                return json_encode(array('status'=>1,'msg'=>lang('successfully'),'data'=>$productList));
            }else{
                return array('status'=>3,'msg'=>lang('no_data'));
            }

        }
    }

    // 添加现有产品做配件
    public function proAddNewJump_itemAdd(){
        if(isset($this->data['request']) && $this->data['request'] == "insert"){
            unset($this->data['request']);
            // return $this->data;
            if(isset($this->data['pro_id']) && $this->data['pro_id']){
                if(isset($this->data['prochar_id']) && $this->data['prochar_id']){
                    if(isset($this->data['pro_id2item']) && $this->data['pro_id2item']){
                        if(isset($this->data['prochar_id2item']) && $this->data['prochar_id2item']){
                            if(isset($this->data['qty']) && $this->data['qty']){

                                $prochar2item = getValue('prochar',['id'=>$this->data['prochar_id2item']],'model');
                                $prochar = getValue('prochar',['id'=>$this->data['prochar_id']],'model');

                                $has = hasData('pro_item',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'pro_id2item'=>$this->data['pro_id2item'],'prochar_id2item'=>$this->data['prochar_id2item']]);
                                if($has){
                                    return json_encode(array('status'=>3,'msg'=>lang('the_role_of_product').'：'.$prochar.' '.lang('accessory').'：'.$prochar2item.' '.lang('already_exists')));
                                }else{

                                    $res = insertData('pro_item',$this->data);
                                    if($res){
                                        return json_encode(array('status'=>1,'msg'=>lang('the_role_of_product').'：'.$prochar.' '.lang('accessory').'：'.$prochar2item.' '.lang('edited_successfully')));
                                    }else{
                                        return array(array('status'=>0,'msg'=>lang('the_role_of_product').'：'.$prochar.' '.lang('accessory').'：'.$prochar2item.' '.lang('edit_failed')));
                                    }
                                }
                            }else{
                                return json_encode(array('status'=>2,'msg'=>lang('missing_quantity')));
                                }
                        }else{
                            return json_encode(array('status'=>2,'msg'=>lang('missing_role_accessory_product')));
                        }
                    }else{
                        return json_encode(array('status'=>2,'msg'=>lang('missing_accessory_product')));
                    }
                }else{
                    return json_encode(array('status'=>2,'msg'=>lang('missing_role_product')));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('missing_product')));
            }
        }else if(isset($this->data['request']) && $this->data['request'] == "add"){
            // 获取角色和角色特征信息
            $prochar = $this->getItemChar(['id,model,ctn_note'],['pro_id'=>$this->data['pro_id']]);
            // 产品分类
            $procat = getSearch('procat','zu',['zu.id,zu.pid,mu.content'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
            $procat = getTree($procat);
            return view('itemaddopen',['title'=>'添加现有产品做配件','type'=>'add','pro_id'=>$this->data['pro_id'],'procat'=>$procat,'prochar'=>$prochar]);
        }else{
            // dump(321);
            return $this->proAddNewJump_itemAjax($this->data,$this->lang);
        }
    }

    // 编辑现有产品做配件
    public function proAddNewJump_itemEdit(){
        if(isset($this->data['request']) && $this->data['request'] == "update"){
            unset($this->data['request']);
            // return $this->data;
            if(isset($this->data['id']) && $this->data['id']){
                if(isset($this->data['pro_id']) && $this->data['pro_id']){
                    if(isset($this->data['prochar_id']) && $this->data['prochar_id']){
                        if(isset($this->data['pro_id2item']) && $this->data['pro_id2item']){
                            if(isset($this->data['prochar_id2item']) && $this->data['prochar_id2item']){
                                if(isset($this->data['qty']) && $this->data['qty']){
                                    $prochar2item = getValue('prochar',['id'=>$this->data['prochar_id2item']],'model');
                                    $prochar = getValue('prochar',['id'=>$this->data['prochar_id']],'model');
                                    $res = updateData('pro_item',['id'=>$this->data['id']],$this->data);
                                    if($res >=0){
                                        return json_encode(array('status'=>1,'msg'=>lang('the_role_of_product').'：'.$prochar.' '.lang('accessory').'：'.$prochar2item.' '.lang('edited_successfully')));
                                    }else{
                                        return json_encode(array('status'=>0,'msg'=>lang('the_role_of_product').'：'.$prochar.' '.lang('accessory').'：'.$prochar2item.' '.lang('edit_failed')));
                                    }
                                }else{
                                    return json_encode(array('status'=>2,'msg'=>lang('missing_quantity')));
                                }
                            }else{
                                return json_encode(array('status'=>2,'msg'=>lang('missing_role_accessory_product')));
                            }
                        }else{
                            return json_encode(array('status'=>2,'msg'=>lang('missing_accessory_product')));
                        }
                    }else{
                        return json_encode(array('status'=>2,'msg'=>lang('missing_role_product')));
                    }
                }else{
                    return json_encode(array('status'=>2,'msg'=>lang('missing_product')));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('missing_product')));
            }
                
        }else if(isset($this->data['request']) && $this->data['request'] == "edit"){
            // 根据被编辑的配件Id 获取配件信息
            $item = getDatas('pro_item',['*'],['id'=>$this->data['item_id']])[0];
            // 获取角色和角色特征信息
            $prochar = $this->getItemChar(['*'],['pro_id'=>$item['pro_id'],'id'=>$item['prochar_id']]);
            // 配件产品信息
            $itempro = getSearch('pro','zu',['zu.*,mu.content'],['zu.id'=>$item['pro_id2item'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
            // 配件角色信息
            $itemprocharmodel = getValue('prochar',['id'=>$item['prochar_id2item']],'model');
            if($itemprocharmodel){
                $itemprochar['id'] = $item['prochar_id2item'];
                $itemprochar['content'] = $item['prochar_id2item'].'/'.$itemprocharmodel.'/'.$itempro['content'];
            }else{
                $itemprochar['id'] = $item['prochar_id2item'];
                $itemprochar['content'] = $item['prochar_id2item'].'/'.$itempro['content'];
            }
            // dump($pro);die;
            // 产品分类
            $procat = getSearch('procat','zu',['zu.id,zu.pid,mu.content'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
            $procat = getTree($procat);
            return view('itemaddopen',['title'=>lang('edit_accessory'),'type'=>'edit','pro_id'=>$item['pro_id'],'procat'=>$procat,'prochar'=>$prochar,'itempro'=>$itempro,'itemprochar'=>$itemprochar,'qty'=>$item['qty'],'item_id'=>$this->data['item_id']]);
        }else{
            // dump(321);
            return $this->proAddNewJump_itemAjax($this->data,$this->lang);
        }
    }

    public function proAddNewJump_itemDel(){
        // return $this->data;
        if(isset($this->data['id'])){
            $join = [
                ['prochar c_prochar','zu,prochar_id = c_prochar.id'],
                // ['prochar c_item','zu.prochar_id2item = c_item.id']
            ];
            $join = [
                    ['prochar pc','zu.prochar_id = pc.id'],
                    ['prochar pt','zu.prochar_id2item = pt.id'],
                ];
            $content = getSearch('pro_item','zu',['pc.model prochar_model,pt.model prochar2item_name'],['zu.id'=>$this->data['id']],$join,'zu.id desc')[0];
            // return $content;
            $res = deleteData('pro_item',['id'=>$this->data['id']]);
            if($res){
                return json_encode(array('status'=>1,'msg'=>lang('the_product_model_is').'：'.$content['prochar_model'].' '.lang('delete').' '.$content['prochar2item_name'].' '.lang('cleared_successfully')));
            }else{
                return json_encode(array('status'=>0,'msg'=>lang('the_product_model_is').'：'.$content['prochar_model'].' '.lang('delete').' '.$content['prochar2item_name'].' '.lang('clear_failed')));
            }
        }
    }

    // 获取产品角色的特征
    function getItemChar($field=['*'],$where)
    {
        $prochar = getMultiLangName('prochar',$field,$where,'id desc');
        foreach ($prochar as $k => $v) {
            $feature_ids = getDatas('profeature2char',['profeature_value_id'],['prochar_id'=>$v['id']]);
            $prochar[$k]['feature_zh'] = $prochar[$k]['model'].' / ';
            $prochar[$k]['feature_en'] = $prochar[$k]['model'].' / ';
            $prochar[$k]['feature_pt'] = $prochar[$k]['model'].' / ';
            if(!empty($feature_ids)){
                foreach ($feature_ids as $k1 => $v1) {
                    $feature = getMultiLangName('proattr_value',['*'],['id'=>$v1['profeature_value_id']],'id desc')[0];
                    $prochar[$k]['feature_zh'] .= $feature['name_zh'].' / ';
                    $prochar[$k]['feature_en'] .= $feature['name_en'].' / ';
                    $prochar[$k]['feature_pt'] .= $feature['name_pt'].' / ';
                }
            }
        }
        return $prochar;
    }

    //  生成新的产品做配件
    public function proAddNewJump_itemNewAdd(){
        if(isset($this->data['request']) && $this->data['request'] == "insert"){
            
            // return $pro_name;
            if(!empty($this->data['data'])){

                // 获取产品名称，在组合产品配件多语言名称时使用 注1
                $pro_multi = getMultiLangName('multi_lang',['*'],['mkey'=>'pro_'.$this->data['pro_id']],'id desc')[0];
                $brand_id = getValue('pro',['id'=>$this->data['pro_id']],'brand_id');
                            // dump($this->data['data']);die;
                foreach ($this->data['data'] as $k=> $v) {
                    // 判断是否通用
                    if($v['is_public'] != 1){
                        // 生成一个新的产品
                        $proinsert['brand_id'] = $brand_id;
                        $proinsert['is_item'] = 1;
                        $proinsert['create_id'] = $this->user_id;
                        $proinsert['create_at'] = time();
                        $pro = insertData('pro',$proinsert);
                        if($pro){
                            // 编辑新生成产品的mkey
                            $proupdate = updateData('pro',['id'=>$pro],['mkey'=>'pro_'.$pro]);
                            // 根据配件类型ID获取配件类型名称，
                            $pro_name = getDatas('multi_lang',['*'],['mkey'=>'pro_item_type_'.$v['itemcat']]);
                            // 新增产品角色，
                            $procharinsert = insertData('prochar',[
                                                                    'model'=>'',
                                                                    'package_qty'=>$v['package_qty'],
                                                                    'ctn_kgs'=>$v['ctn_kgs'],
                                                                    'ctn_size'=>$v['ctn_size'],
                                                                    'pro_id'=>$pro,
                                                                    'create_id'=>$this->user_id,
                                                                    'create_at'=>time()
                                                                ]);
                            // 注1
                            foreach ($pro_name as $k1 => $v1) {
                                $pro_nameinsert = insertData('multi_lang',['lang'=>$v1['lang'],'mkey'=>'pro_'.$pro,'content'=>$pro_multi['name_'.$v1['lang']].$v1['content']]);
                            }
                        }
                        if($this->data['prochar'] == 'all'){
                            $prochar_ids = getDatas('prochar',['id'],['pro_id'=>$this->data['pro_id']]);
                            // dump($prochar_ids);die;
                            if(!empty($prochar_ids)){
                                foreach ($prochar_ids as $key => $value) {
                                    $pro_item = insertData('pro_item',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$value['id'],'pro_id2item'=>$pro,'prochar_id2item'=>$procharinsert,'qty'=>$v['item_qty']]);
                                }
                            }
                        }else{
                            $pro_item = insertData('pro_item',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar'],'pro_id2item'=>$pro,'prochar_id2item'=>$procharinsert,'qty'=>$v['item_qty']]);
                        }
                        
                    }else{
                        // 如果是通用的 直接获取配件类型中的pri_id和prochar_id 进行绑定
                        $pro_item_cat = getDatas('pro_item_type',['*'],['id'=>$v['itemcat']])[0];
                        if($this->data['prochar'] == 'all'){
                            $prochar_ids = getDatas('prochar',['id'],['pro_id'=>$this->data['pro_id']]);
                            if(!empty($prochar_ids)){
                                foreach ($prochar_ids as $key => $value) {
                                    $pro_item = insertData('pro_item',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$value['id'],'pro_id2item'=>$pro_item_cat['pro_id'],'prochar_id2item'=>$pro_item_cat['prochar_id'],'qty'=>$v['item_qty']]);
                                }
                            }
                        }else{
                            $pro_item = insertData('pro_item',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar'],'pro_id2item'=>$pro_item_cat['pro_id'],'prochar_id2item'=>$pro_item_cat['prochar_id'],'qty'=>$v['item_qty']]);
                        }
                        
                    }
                }
                if($pro_item){
                    return json_encode(array('status'=>1,'msg'=>'added_successfully'));
                }else{
                    return json_encode(array('status'=>0,'msg'=>'add_failed'));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
            
        }else{
            // 获取角色和角色特征信息
            $prochar = $this->getItemChar(['id,model,ctn_note'],['pro_id'=>$this->data['pro_id']]);
            // 获取配件分类信息
            // $proitemcat = getMultiLangName('pro_item_type',['*'],'1=1','id desc');
            $proitemcat = getSearch('pro_item_type','zu',['zu.*,mu.content'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id asc');
            // dump($proitemcat);die;
            return view('itemnewaddopen',['title'=>lang('generate_new_products_as_accessories'),'pro_id'=>$this->data['pro_id'],'prochar'=>$prochar,'proitemcat'=>$proitemcat]);
        }
    }

    // 多图产品上传
    public function proAddNewJump_multiImgAdd(){
        if(isset($this->data['request']) && $this->data['request'] == "insert"){

        }else{
            $join = [
                        ['multi_lang zu_mu','zu.mkey = zu_mu.mkey'],
                        // ['pro_img i','zu.id = i.pro_id'],
                        ['pro_brand b','zu.brand_id = b.id'],
                    ];

            $field = [
                'zu.*,b.name brand_name,zu_mu.content pro_name'
            ];
            $where['zu.id'] = $this->data['pro_id'];
            $where['zu_mu.lang'] = $this->lang;
            $productList = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
            return view('proaddnew_multiimgopen',['title'=>lang('multi-picture_upload'),'pro_id'=>$this->data['pro_id'],'productList'=>$productList]);
        }
    }

    // 上传产品图片
    public function proAddNewJump_imgAdd(){
        if(isset($this->data['request']) && $this->data['request'] == "insert"){

        }else{
            $join = [
                        ['multi_lang zu_mu','zu.mkey = zu_mu.mkey'],
                        // ['pro_img i','zu.id = i.pro_id'],
                        ['pro_brand b','zu.brand_id = b.id'],
                    ];

            $field = [
                'zu.*,b.name brand_name,zu_mu.content pro_name'
            ];
            $where['zu.id'] = $this->data['pro_id'];
            $where['zu_mu.lang'] = $this->lang;
            $productList = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
            $procharlist = $this->getItemChar(['id,model,mkey'],['pro_id'=>$this->data['pro_id']]);
            if(!empty($procharlist)){
                foreach ($procharlist as $k => $v) {
                    $procharlist[$k]['content'] = $v['feature_'.$this->lang];
                }
            }
            if(isset($this->data['img_id']) && $this->data['img_id']){
                $imgInfo = getDatas('pro_img',['*'],['id'=>$this->data['img_id']])[0];
            }else{
                $imgInfo = [];
            }
            // dump($procharlist);die;
            return view('proaddnew_imgopen',['title'=>lang('upload_product_image'),'request'=>'add','pro_id'=>$this->data['pro_id'],'productList'=>$productList,'procharlist'=>$procharlist,'imgInfo'=>$imgInfo]);
        }
    }

    //  编辑图片
    public function proAddnew_imgedit(){
        if (isset($this->data['request']) && $this->data['request'] == 'edit') {
            $join = [
                        ['multi_lang zu_mu','zu.mkey = zu_mu.mkey'],
                        // ['pro_img i','zu.id = i.pro_id'],
                        ['pro_brand b','zu.brand_id = b.id'],
                    ];

            $field = [
                'zu.*,b.name brand_name,zu_mu.content pro_name'
            ];
            $where['zu.id'] = $this->data['pro_id'];
            $where['zu_mu.lang'] = $this->lang;
            $productList = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
            $procharlist = $this->getItemChar(['id,model,mkey'],['pro_id'=>$this->data['pro_id']]);
            if(!empty($procharlist)){
                foreach ($procharlist as $k => $v) {
                    $procharlist[$k]['content'] = $v['feature_'.$this->lang];
                }
            }
            if(isset($this->data['img_id']) && $this->data['img_id']){
                $imgInfo = getDatas('pro_img',['*'],['id'=>$this->data['img_id']])[0];
            }else{
                $imgInfo = [];
            }
            // dump($procharlist);die;
            return view('proaddnew_imgopen',['title'=>lang('edit_product/_role_image'),'request'=>'edit','pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'productList'=>$productList,'procharlist'=>$procharlist,'imgInfo'=>$imgInfo]);
        }else if (isset($this->data['request']) && $this->data['request'] == 'update') {
            return $this->data;
        }
    }


    // 上传图片
    public function imgUpdate(Request $request)
    {   
        // return $this->data;
        if(isset($this->data['pro_id']) && $this->data['pro_id']){
            $pro_name = getValue('multi_lang',['lang'=>$this->lang,'mkey'=>'pro_'.$this->data['pro_id']],'content');
            if(isset($this->data['prochar_id']) && $this->data['prochar_id']){
                $prochar_id = $this->data['prochar_id'];
                $procharmodel = getValue('prochar',['id'=>$this->data['prochar_id']],'model');
            }else{
                $prochar_id = 0;
                $procharmodel = '';
            }
            
            // 图片类型
            if(isset($this->data['is_icon']) && $this->data['is_icon']){
                $is_icon = $this->data['is_icon'];
            }else{

                $is_icon = 2;
            }

            // 存在img_id 则编辑替换
            if(isset($this->data['img_id']) && $this->data['img_id'] != 'undefined'){
                $img_id = $this->data['img_id'];
                // 如果 提交数据is_icon  则不管原来的is_icon是什么状态 都按提交的状态
                if(!isset($this->data['is_icon'])) $is_icon = getValue('pro_img',['id'=>$this->data['img_id']],'is_icon');
                $img_oldname = getValue('pro_img',['id'=>$this->data['img_id']],'drank');
            }else{
                $img_id = 0;
                $img_oldname = 0;
            }


            $drank = Db::name('pro_img')->where(['pro_id'=>$this->data['pro_id'],'prochar_id'=>0,'width'=>1000])->max('drank');
        // return $is_icon;

            if(count($request->file('file')) > 0 ){
                $fileUpdate = new File();
                $pathName = 'static/Admin/img/pro/'.$this->data['pro_id'];
                $backFile = $fileUpdate->multiAndSingleUpload($request->file('file'),$pathName,$this->data['pro_id'],$prochar_id,$img_oldname,$drank);
                // dump($backFile);die;
                if(!empty($backFile)){
                    $res = $this->insertMysqlImg($backFile,'/uploads/'.$pathName,$this->data['pro_id'],$prochar_id,$is_icon,$img_id,$drank);
                }
                if($res == 2000){
                    return json_encode(array('status'=>1,'msg'=>$pro_name.'->'.$procharmodel.' '.lang('added_successfully')));
                }else{
                    return json_encode(array('status'=>0,'msg'=>$pro_name.'->'.$procharmodel.' '.lang('add_failed')));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>'缺少图片'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'缺少产品数据'));
        }
        return $res;
    }

    function insertMysqlImg($data,$pathName,$pro_id,$prochar_id,$is_icon,$img_id,$drank)
    {
        $old_img_url = getValue('pro_img',['id'=>$img_id],'img_url');
        $whereurl = '1=1';
        if($old_img_url) $whereurl = explode('.', $old_img_url)[0];
        if(!empty($data[0]['info'])){
            foreach ($data as $k => $v) {
                $insertMysqlImg[0]['pro_id'] = $pro_id;
                $insertMysqlImg[0]['prochar_id'] = $prochar_id;
                $insertMysqlImg[0]['is_icon'] = $is_icon;
                $insertMysqlImg[0]['size'] = $v['info']->getInfo('size');
                $insertMysqlImg[0]['width'] = 1000;
                $insertMysqlImg[0]['height'] = 1000;
                $insertMysqlImg[0]['img_url'] = str_replace("\\","/",substr($v['info']->getpathName(),strripos($v['info']->getpathName(),"uploads")-1));
                $insertMysqlImg[0]['img_format'] = pathinfo($v['info']->getFilename(), PATHINFO_EXTENSION);
                $insertMysqlImg[0]['drank'] = $v['drank'];
                if(isset($v['thumb'])){
                    foreach ($v['thumb'] as $k1 => $v1) {
                        $insertMysqlImg[$k1]['pro_id'] = $pro_id;
                        $insertMysqlImg[$k1]['prochar_id'] = $prochar_id;
                        $insertMysqlImg[$k1]['is_icon'] = 1;
                        $insertMysqlImg[$k1]['size'] = 1;
                        $insertMysqlImg[$k1]['width'] = $v1['width'];
                        $insertMysqlImg[$k1]['height'] = $v1['height'];
                        $insertMysqlImg[$k1]['img_url'] = $v1['img_url'];
                        $insertMysqlImg[$k1]['img_format'] = pathinfo($v1['img_url'], PATHINFO_EXTENSION);
                        $insertMysqlImg[$k1]['drank'] = $k1;
                    }
                }
                $res1 = deleteData('pro_img',['pro_id'=>$pro_id,'img_url'=>'/static/Admin/img/nt_logo.png']);
                foreach ($insertMysqlImg as $k => $v) {
                    if($img_id){
                        $v['update_at'] = time();
                        $v['update_id'] = session('userid') == null ? 1 : session('userid');
                        $res = Db::name('pro_img')->where(['width'=>$v['width'],'img_url'=>['like','%'.$whereurl.'%']])->update($v);
                    }else{
                        $v['create_at'] = time();
                        $v['create_id'] = session('userid') == null ? 1 : session('userid');
                        $res = insertData('pro_img',$v);
                    }
                }
            }
            if ($res >= 0) {
                return 2000;
            }else{
                return 500;
            }
        }
    }

    public function imgDel(){
        if(isset($this->data['id'])){
            $img = getDatas('pro_img',['*'],['id'=>$this->data['id']]);
            $pro_name = getValue('multi_lang',['lang'=>$this->lang,'mkey'=>'pro_'.$img[0]['pro_id']],'content');
            $procharmodel = getValue('prochar',['id'=>$img[0]['prochar_id']],'model');
            if(!empty($img)){
                $whereurl = explode('.', $img[0]['img_url'])[0];
                // 获取img_id下的icon图片一起删除
                $delData = getDatas('pro_img',['*'],['img_url'=>['like','%'.$whereurl.'%']]);
                if(!empty($delData)){
                    foreach ($delData as $k => $v) {
                        $res = deleteData('pro_img',['id'=>$v['id']]);
                        if($v['img_url'] != "/static/Admin/img/nt_logo.png"){
                            $image = \think\Image::open(ROOT_PATH . 'public' . DS . $v['img_url']);
                            // 先释放文件，再进行删除 不然会提示没权限
                            unset($image);  
                            $res1 = unlink(ROOT_PATH . 'public' . DS . $v['img_url']);  
                        }                
                    }
                    if($res){
                        return json_encode(array('status'=>1,'msg'=>$pro_name.'->'.$procharmodel.' '.lang('cleared_successfully')));
                    }else{
                        return json_encode(array('status'=>0,'msg'=>$pro_name.'->'.$procharmodel.' '.lang('clear_failed')));
                    }
                }
                
            }else{
                return json_encode(array('status'=>2,'msg'=>'缺少图片'));
            }
            
        }else{
            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
        }
    }

    // 产品介绍类型
    public function detailtype(Request $request)
    {   
        $prodesc_cat = getSearch('prodesc_cat','zu',['zu.*,mu.content'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc');
        if(!empty($prodesc_cat)){
            foreach ($prodesc_cat as $k => $v) {
                $prodesc_cat[$k]['verify'] = $v['verify'] == 1 ? lang('need') : lang('no_need');
                // 获取介绍类型得支持语言
                $prodesc_cat[$k]['langStr'] = '';
                if($v['lang_ids'] != ''){
                    $lang_idsArr = explode(',', $v['lang_ids']);
                    foreach ($lang_idsArr as $k1 => $v1) {
                        switch ($v1) {
                            case 'zh':
                                $prodesc_cat[$k]['langStr'] .= lang('Chinese').',';
                                break;
                            case 'en':
                                $prodesc_cat[$k]['langStr'] .= lang('English').',';
                                break;
                            case 'pt':
                                $prodesc_cat[$k]['langStr'] .= lang('PORTUGUES').',';
                                break;
                            default:
                                $prodesc_cat[$k]['langStr'] .= lang('others').',';
                                break;
                        }
                    }
                    $prodesc_cat[$k]['langStr'] = rtrim($prodesc_cat[$k]['langStr'],',');
                }
                // 获取当前支持的职务信息
                $prodesc_cat[$k]['position'] = '';
                if($v['position_ids'] != ''){
                    $position_ids = explode(',', $v['position_ids']);
                    foreach ($position_ids as $k2 => $v2) {
                        $prodesc_cat[$k]['position'] .= getSearch('position','zu',['mu.content'],['zu.id'=>$v2,'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0]['content'].'/';
                    }
                    $prodesc_cat[$k]['position'] = rtrim($prodesc_cat[$k]['position'],'/');
                }else{
                    $selfEditData = [];
                }

            }
        }
        
        $returnData['size'] = count( $prodesc_cat);       
        if(isset($this->data['page']) && isset($this->data['limit'])){
            $prodesc_cat = array_slice($prodesc_cat,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
            $returnData['page'] = $this->data['page'];
        }else{
            $returnData['page'] =1;
        }

        if($request->isAjax()){
            $returnData['prodesc_cat'] = $prodesc_cat;
            return json_encode($returnData);
        }else{
            // dump($prodesc_cat);die;
            return view('detailtype',['prodesc_cat'=>$prodesc_cat]);   
        }
    }


    public function detailtypeAdd(){
        if(isset($this->data['request']) && $this->data['request'] == 'insert'){
            // return $this->data;  
            if($this->data['proname']['zh'] == '') return json_encode(array('status'=>2,'msg'=>lang("type_name_can't_bem_empty")));
            if(!isset($this->data['lang'])) return json_encode(array('status'=>2,'msg'=>lang("the_specified_language_can't_be_empty")));
            if(!isset($this->data['rootarr'])) return json_encode(array('status'=>2,'msg'=>lang("authorized_position_can't_be_empty")));

            $prodesc_cat['lang_ids'] = isset($this->data['lang']) ? implode(',', $this->data['lang']) : '';
            $prodesc_cat['position_ids'] = isset($this->data['rootarr']) ? implode(',', $this->data['rootarr']) : '';
            $prodesc_cat['verify'] = $this->data['is_audit'] == 'true' ? 1 : 0;
            $prodesc_cat['create_id'] = $this->user_id;
            $prodesc_cat['create_at'] = time();
            $prodesc_catInsert = insertData('prodesc_cat',$prodesc_cat);
            if($prodesc_catInsert){
                $res = updateData('prodesc_cat',['id'=>$prodesc_catInsert],['mkey'=>'prodesc_cat_'.$prodesc_catInsert]);
                // foreach ($this->data['proname'] as $k => $v) {

                $lang_cate = getLanguage($this->lang);
                foreach ($lang_cate as $k => $v) {
                    $multi = insertData('multi_lang',['mkey'=>'prodesc_cat_'.$prodesc_catInsert,'lang'=>$v['code'],'content'=>$this->data['proname']['zh']]);
                    // 新增产品介绍的内容，占位 后期好编辑修改
                    $ress = insertData('prodesc',['prodesc_cat_id'=>$prodesc_catInsert,'create_id'=>$this->user_id,'create_at'=>time(),'lang'=>$v['code']]);
                }
                if($res && $multi){
                    return json_encode(array('status'=>1,'msg'=>$this->data['proname']['zh'].' '.lang('added_successfully')));
                }else{
                    return json_encode(array('status'=>0,'msg'=>$this->data['proname']['zh'].' '.lang('add_failed')));
                }
            }else{
                return json_encode(array('status'=>0,'msg'=>$this->data['proname']['zh'].' '.lang('add_failed')));
            }        
            
        }else{
            //所有的职位名称
            $positions = getFrames('position',$this->lang,2);
            // dump($positions);die;
            return view('detailtype_open',['title'=>lang('add_product_description_type'),'positions'=>$positions,'request'=>'add']);
        }
    }

    public function detailtypeEdit(Request $request){
        if(isset($this->data['request']) && $this->data['request'] == 'update'){
            // return $this->data;
            foreach ($this->data['proname'] as $k1 => $v1) {
                if($v1 == ''){
                    return json_encode(array('status'=>2,'msg'=>lang("type_name_can't_bem_empty")));
                }
            }
            if(!isset($this->data['lang'])) return json_encode(array('status'=>2,'msg'=>lang("the_specified_language_can't_be_empty")));
            if(!isset($this->data['rootarr'])) return json_encode(array('status'=>2,'msg'=>lang("authorized_position_can't_be_empty")));

            $prodesc_cat['lang_ids'] = isset($this->data['lang']) ? implode(',', $this->data['lang']) : '';
            $prodesc_cat['position_ids'] = isset($this->data['rootarr']) ? implode(',', $this->data['rootarr']) : '';
            $prodesc_cat['verify'] = $this->data['is_audit'] == 'true' ? 1 : 0;
            // return $prodesc_cat;
            $prodesc_catUpdate = updateData('prodesc_cat',['id'=>$this->data['id']],$prodesc_cat);
            if($prodesc_catUpdate >= 0){
                $content = '';
                foreach ($this->data['proname'] as $k => $v) {
                    $multi = updateData('multi_lang',['mkey'=>'prodesc_cat_'.$this->data['id'],'lang'=>$k],['content'=>$v]);
                    if($k == $this->lang) $content = $v;
                }
                if($multi>= 0){
                    return json_encode(array('status'=>1,'msg'=>$content.' '.lang('edited_successfully')));
                }else{
                    return json_encode(array('status'=>0,'msg'=>$content.' '.lang('edit_failed')));
                }
            }else{
                return json_encode(array('status'=>0,'msg'=>lang('edit_failed')));
            }
        }else{
                
                // 被编辑的信息
                $editData = getMultiLangName('prodesc_cat',['*'],['id'=>$this->data['id']],'id asc')[0];
                // 被编辑的职务信息
                if($editData['position_ids'] != ''){
                    $selfposition_ids = explode(',', $editData['position_ids']);
                }else{
                    $selfposition_ids = [];
                }
                // dump($selfposition_ids);die;
                // 被编辑的介绍类型支持的语言
                if($editData['lang_ids'] != ''){
                    $lang_ids = explode(',', $editData['lang_ids']);
                }else{
                    $lang_ids = [];
                }
                // 所有职务信息
                // $positions = getFrames('position',$this->lang,$this->data['count']);
                // dump($positions);die;

            if(isset($this->data['request']) && $this->data['request'] == 'edit'){
                $admin = session('admin');
                if ($admin['position_id'] != 1) {
                    $admin_countryid = getinfo($admin['position_id'], 'position')['country_id'];
                }else{
                    $admin_countryid=0;
                }
                $returnData['position_ids'] = $selfposition_ids;
                $returnData['country_id'] = $admin_countryid;
                return json_encode($returnData);
            }else{
                return view('detailtype_open',['title'=>lang('edit_product_introduction_type'),'request'=>'edit','editData'=>$editData,'lang_ids'=>$lang_ids,'id'=>$this->data['id']]);
            }
            
            
        }
    }

    public function detailtypeDel()
    {   
        // return $this->data;
        if($this->data['id']){
            $res = deleteData('prodesc_cat',['id'=>$this->data['id']]);
            $content = getValue('multi_lang',['mkey'=>'prodesc_cat_'.$this->data['id'],'lang'=>$this->lang],'content');
            if($res){
                $ress = deleteData('multi_lang',['mkey'=>'prodesc_cat_'.$this->data['id']]);
                if($ress){
                    return json_encode(array('status'=>1,'msg'=>$content.' '.lang('cleared_successfully')));
                }else{
                    return json_encode(array('status'=>0,'msg'=>$content.' '.lang('clear_failed')));
                }
            }else{
                return json_encode(array('status'=>0,'msg'=>$content.' '.lang('clear_failed')));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
        }
    }


    // 获取当前类行下 所以能用的子级
    function getChildID($table,$field,$where,$pid=0)
    {   
        $_data = Db::name($table)
                    ->field($field)
                    ->where($where)
                    ->where('pid',$pid)
                    // ->fetchSql()
                    ->select();
                // return $_data;
        if(!empty( $_data)){
            foreach ($_data as $k => $v) {
                array_push($this->getChildIDdata,$v['id']);
                $res = $this->getChildID($table,$field,$where,$v['id']);
            }

            return $this->getChildIDdata;
        }else{
            return $this->getChildIDdata;
        }
    }


        // 批量/单个删除
    public function batchDel()
    {
        return $this->changeStatus($this->data,$this->lang);
    }

    // 批量/单个下架
    public function batchObtained()
    {
        return $this->changeStatus($this->data,$this->lang);
    }

    // 批量/单个恢复
    public function batchRestore()
    {
        return $this->changeStatus($this->data,$this->lang);
    }

    // 批量/单个清除
    public function batchClear()
    {
        return $this->changeStatus($this->data,$this->lang);
    }


    // 下架/批量下架，软删除/批量软删除，恢复/批量恢复，清除/批量清除
    public function changeStatus($data,$lang){
        // return $this->data;
        if(!empty($this->data['id'])){  // 判断条件是否达成
            if($data['fun'] == 'update'){
                $updateData = ['status'=>$data['value']];
                Db::startTrans();
                    $content = '';
                    if(is_array($data['id'])){    // 是否批量删除
                        foreach ($data['id'] as $k => $v) {
                            $res = updateData($data['table'],['id'=>$v],$updateData);
                            $content .= ','.getValue('multi_lang',['mkey'=>'pro_'.$v,'lang'=>$lang],'content');
                        }
                        $content = substr($content,1);
                        if($res){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>lang('product_name').'：'.$content.' '.lang(getViewMultiLanguage($data['value'])).' '.lang('successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>lang('product_name').'：'.$content.' '.lang(getViewMultiLanguage($data['value'])).' '.lang('failed')));
                        }
                    }else{
                        $res = updateData($data['table'],['id'=>$data['id']],$updateData);
                        $content .= getValue('multi_lang',['mkey'=>'pro_'.$data['id'],'lang'=>$lang],'content');
                        if($res){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>lang('product_name').'：'.$content.' '.lang(getViewMultiLanguage($data['value'])).' '.lang('successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>lang('product_name').'：'.$content.' '.lang(getViewMultiLanguage($data['value'])).' '.lang('failed')));
                        }
                    }
            }else if($data['fun'] == 'deletes'){     // 清除

                $content = '';
                if(is_array($data['id'])){    // 是否批量删除
                    foreach ($data['id'] as $k => $v) {  
                        $content .= getValue('multi_lang',['mkey'=>'pro_'.$v,'lang'=>$lang],'content');
                        $res = $this->requestDel($v,$content);
                    }
                }else{
                    $content .= getValue('multi_lang',['mkey'=>'pro_'.$data['id'],'lang'=>$lang],'content');
                    $res = $this->requestDel($data['id'],$content);
                }
                return $res;
            }
        }else{
            return json_encode(array('status' => 500,'msg'=>'请勾选需要操作的产品'));
        }

    }

    // 删除fun
    function requestDel($id,$content){
        Db::startTrans();
        try{
            $has = hasData('buy_plan',['pro_id'=>$id]);
            if( !$has ){  // 判断是否绑定采购计划订单
                // 删除旗下所有子级
                $res = deleteData('pro',['id'=>$id],$idArray = array());
                if($res){
                    // 清除产品简介
                    $res_ = deleteData('prodesc',['pro_id'=>$id]);
                    // 清除产品多语言（产品名称）
                    $res1 = deleteData('multi_lang',['mkey'=>'pro_'.$id]);
                    // 清除产品绑定的产品类型
                    $res5 = deleteData('pro2cate',['pro_id'=>$id]);
                    // dump($res1);die;
                    if($res_ && $res1 && $res5){
                        // 是否拥有产品角色
                        $has_char = getDatas('prochar',['id,mkey'],['pro_id'=>$id],'id desc');
                        if(!empty($has_char)){
                            foreach ($has_char as $k1 => $v1) {
                                // 清除产品角色
                                $res2 = deleteData('prochar',['id'=>$v1['id']]);
                                // 清除产品角色多语言信息（产品角色包装说明）
                                $res3 = deleteData('multi_lang',['mkey'=>'prochar_note_'.$v1['id']]);
                                // 清除产品角色的特征
                                $res4 = deleteData('profeature2char',['prochar_id'=>$v1['id']]);
                            }
                            if($res2 && $res3){
                                Db::commit();
                                return json_encode(array('status'=>1,'msg'=>$content.' '.lang('cleared_successfully')));
                            }else{
                                Db::rollback();
                                return json_encode(array('status'=>0,'msg'=>$content.' '.lang('clear_failed').'1'));
                            }
                        }
                    }else{
                        Db::rollback();
                        return json_encode(array('status'=>0,'msg'=>$content.' '.lang('clear_failed').'2'));
                    }
                    
                    
                }else{
                    Db::rollback();
                    return json_encode(array('status'=>0,'msg'=>$content.' '.lang('clear_failed').'3'));
                }

            }else{
                return json_encode(array('status'=>2,'msg'=>lang('clear_failed').' '.lang('planned_purchase_order_was_generated_for_current_product')));
            }
        }catch(\Exception $e){
            Db::rollback();
            return json_encode(array('status'=>3,'msg'=>$e));
        }
    }

    // 配件编辑状态
    public function itemChangeStatus(){
        if(isset($this->data['id']) && $this->data['id']){
            $res = updateData('pro_item',['id'=>$this->data['id']],['status'=>$this->data['status']]);
        }else{
            return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
        }
    }
}
